说明
本篇主要讲述两个问题
1、前后端分离项目中的传参接收为NULL的处理
2、传参处理完成后swagger打开报“Whitelabel Error Page No mapping found for HTTP request with URI [/doc.html] in DispatcherServlet with name dispatcherServlet”错
3、自定义JSON解析器后产生LocalDate和LocalDateTime时间格式错乱问题处理
1、前后端分离项目中的传参接收为NULL的处理
如:
前端传参
"oId": ""
后端参数定义
private String oId;
参数接收结果为NULL
测试发现
前端传参为:"oid"时,后端可以正常接收到参数
翻阅资料得知:SpringBoot默认Jackson解析json大小写转换不一致,会把大写转换为小写,导致接收到的参数为NULL。
查看:SpringBoot的源码可以看到,如果我们没有自定义json解析器,则SpringBoot会创建一个默认值
那么我们自定义一个JSON解析器覆盖默认的即可
1)定义JSON解析器
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class JacksonUtils extends ObjectMapper{
private static final JacksonUtils intance = new JacksonUtils();
public static JacksonUtils getInstance() {
return intance;
}
private JacksonUtils() {
this(JsonInclude.Include.NON_NULL);
}
private JacksonUtils(JsonInclude.Include include) {
if (include != null) {
this.setSerializationInclusion(include);
}
this.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
this.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
this.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
this.enable(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS);
this.enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS);
this.enable(JsonParser.Feature.ALLOW_COMMENTS);
this.enable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
this.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
this.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
this.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
this.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
this.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
this.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString("");
}
});
this.registerModule((new SimpleModule()).addSerializer(String.class, new JsonSerializer<String>() {
public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
}
}));
this.setTimeZone(TimeZone.getDefault());
this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
}
覆盖默认解析器
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
@Configuration
public class WebMVCConfig extends WebMvcConfigurationSupport {
/**
* 自定义输出json格式
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setObjectMapper(JacksonUtils.getInstance());
converters.add(jsonConverter);
super.addDefaultHttpMessageConverters(converters);
}
}
到这里参数接收的问题就得到了解决。
参考:链接
2、传参处理完成后swagger打开报错“Whitelabel Error Page”
刚开始以为是swagger出错了,没有找到对应问题,然后想到前两天修改的配置
,尝试去掉配置发现swagger可以正常使用。
后面注意到项目里面已经实现了WebMvcConfigurer方法
而上述方法又继承了WebMvcConfigurationSupport,搜索知道
1、WebMvcConfigurationSupport 和 WebMvcConfigurer同时使用的时候,只有WebMvcConfigurationSupport中的配置会生效
2、一个项目中只有一个WebMvcConfigurationSupport 类会生效
3、一个项目中可以有多个实现WebMvcConfigurer的类,并且都会生效
综上所述,建议在项目中使用实现WebMvcConfigurer
因为引入了WebMvcConfigurationSupport,而导致WebMvcConfigurer的实现没有生效,引起的swagger配置无用
解决方案:
把两个配置合并成一个实现即可。
注意两者对JSON解析器覆盖方法不一样,
在WebMvcConfigurer中
@Configuration
public class CustomMvcConfig implements WebMvcConfigurer {
/**
* 自定义输出json格式
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setObjectMapper(JacksonUtils.getInstance());
converters.add(jsonConverter);
}
}
而WebMvcConfigurationSupport中使用
@Configuration
public class WebMVCConfig extends WebMvcConfigurationSupport {
/**
* 自定义输出json格式
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setObjectMapper(JacksonUtils.getInstance());
converters.add(jsonConverter);
super.addDefaultHttpMessageConverters(converters);
}
}
3、自定义JSON解析器后产生LocalDate和LocalDateTime时间格式错乱问题处理
之前以为自定义Josn解析器后问题解决,当时也没有发现其他问题,直到第二天,页面频繁查不到数据,看后端有返回数据,但是前端没有做展示,仔细排产后发现是时间格式错乱导致前端解析数据出错,时间格式返回如下:
而VO中定义的时间类型一般都是以下两种
@ApiModelProperty("最后更新时间")
private LocalDate updateTime;
@ApiModelProperty("最后更新时间")
private LocalDateTime updateTime;
然后在字段中加**@JsonFormat**注解后问题仍未解决,结合查阅资料想到是否是JSON自定义解析器导致的,然后在WebMvcConfiguration(以下方法也适用WebMvcConfigurationSupport的继承)中添加时间按格式转换功能extendMessageConverters,实现如下:
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//将java对象序列化为json数据,为消息转换器设置序列转换器
converter.setObjectMapper(new JacksonObjectMapper());
//将自己的消息转换器加入容器中
converters.add(0, converter);
}
注意一定要放到实现方法的第一个,让其优先生效
再定义类JacksonObjectMapper
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
/**
* 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
*/
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_FORMAT_NO_SECOND = "yyyy-MM-dd HH:mm";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注册功能模块 例如,可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}
**先实现反序列化,再进行序列化。**到这里所有问题得到了解决。
后续所有的LocalDate和LocalDateTime时间格式转化也省略了在VO上添加@JsonFormat的繁琐工作。
最后的用自己的血泪史告诫大家,这些工作最好再项目前期就完成,当项目快结束的时候再去改配置,会特别难受的。