项目构建
This commit is contained in:
28
demo-common/demo-common-base/pom.xml
Normal file
28
demo-common/demo-common-base/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-base</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.yxt.demo.common.base.config.handler;
|
||||
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/5/13 17:23
|
||||
* @Description
|
||||
*/
|
||||
public class CustomException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
public CustomException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public CustomException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
*/
|
||||
public CustomException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
*/
|
||||
public CustomException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.yxt.demo.common.base.config.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/10 14:42
|
||||
* @Description
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (SpringUtil.applicationContext == null) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取applicationContext
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
// 通过name获取 Bean.
|
||||
public static Object getBean(String name) {
|
||||
return getApplicationContext().getBean(name);
|
||||
}
|
||||
|
||||
// 通过class获取Bean.
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
|
||||
// 通过name,以及Clazz返回指定的Bean
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return getApplicationContext().getBean(name, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过类型获取Spring容器中的对象
|
||||
*
|
||||
* @param type
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
|
||||
Map<String, T> beansOfType = getApplicationContext().getBeansOfType(type);
|
||||
return beansOfType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过类型获取Spring容器中的对象
|
||||
*
|
||||
* @param type
|
||||
* @param includeNonSingletons
|
||||
* @param allowEagerInit
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
Map<String, T> beansOfType = getApplicationContext().getBeansOfType(type, includeNonSingletons, allowEagerInit);
|
||||
return beansOfType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.yxt.demo.common.base.config.utils.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/5/13 17:15
|
||||
* @Description
|
||||
*/
|
||||
public class CustomizeNullJsonSerializer {
|
||||
|
||||
/**
|
||||
* 处理数组集合类型的null值
|
||||
*/
|
||||
public static class NullArrayJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeStartArray();
|
||||
jsonGenerator.writeEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字符串类型的null值
|
||||
*/
|
||||
public static class NullStringJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeString("");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数值类型的null值
|
||||
*/
|
||||
public static class NullNumberJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeNumber(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理boolean类型的null值
|
||||
*/
|
||||
public static class NullBooleanJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理实体对象类型的null值
|
||||
*/
|
||||
/* public static class NullObjectJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeEndObject();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.yxt.demo.common.base.config.utils.jackson;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/5/13 17:13
|
||||
* @Description
|
||||
*/
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(ObjectMapper.class)
|
||||
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
|
||||
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
|
||||
/** 为objectMapper注册一个带有SerializerModifier的Factory */
|
||||
objectMapper.setSerializerFactory(objectMapper.getSerializerFactory()
|
||||
.withSerializerModifier(new MyBeanSerializerModifier()));
|
||||
|
||||
SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
|
||||
// serializerProvider.setNullValueSerializer(new CustomizeNullJsonSerializer
|
||||
// .NullObjectJsonSerializer());
|
||||
return objectMapper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.yxt.demo.common.base.config.utils.jackson;
|
||||
|
||||
import com.fasterxml.jackson.databind.BeanDescription;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/4/29 18:58
|
||||
* @Description
|
||||
*/
|
||||
public class MyBeanSerializerModifier extends BeanSerializerModifier {
|
||||
|
||||
@Override
|
||||
public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
|
||||
BeanDescription beanDesc,
|
||||
List<BeanPropertyWriter> beanProperties) {
|
||||
// 循环所有的beanPropertyWriter
|
||||
for (int i = 0; i < beanProperties.size(); i++) {
|
||||
BeanPropertyWriter writer = beanProperties.get(i);
|
||||
// 判断字段的类型,如果是数组或集合则注册nullSerializer
|
||||
/* if (isArrayType(writer)) {
|
||||
// 给writer注册一个自己的nullSerializer
|
||||
writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullArrayJsonSerializer());
|
||||
}*/
|
||||
if (isStringType(writer)) {
|
||||
writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullStringJsonSerializer());
|
||||
}
|
||||
}
|
||||
return beanProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是数组
|
||||
*/
|
||||
private boolean isArrayType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是String
|
||||
*/
|
||||
private boolean isStringType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是数值类型
|
||||
*/
|
||||
private boolean isNumberType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return Number.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是boolean
|
||||
*/
|
||||
private boolean isBooleanType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return clazz.equals(Boolean.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user