序言:
前后端联调时,我后台是用对象接收参数的,这时候出现了一个问题,前端少传了参数没问题,但是多传了字段就报400 参数异常错误。一番了解时发现,这是因为spring默认是开启了序列化属性校验,所以我要让spring对多出的参数进行忽略,这样就解决问题了。
经过一番资料查询,发现解决办法有两个
一、yml文件中进行配置
spring:
jackson:
#日期格式化
date-format: yyyy-MM-dd HH:mm:ss
serialization:
#格式化输出
indent_output: true
#忽略无法转换的对象
fail_on_empty_beans: false
#设置空如何序列化
defaultPropertyInclusion: NON_EMPTY
deserialization:
#允许对象忽略json中不存在的属性
fail_on_unknown_properties: false
parser:
#允许出现特殊字符和转义符
allow_unquoted_control_chars: true
#允许出现单引号
allow_single_quotes: true
但是不知道为什么,我配置了 serialization:spring.jackson.fail_on_unknown_properties=false这一项还是没有效果,多传参数依然报错,有知道的大佬请不吝赐教!抱拳~
二、配置文件配置
jacksonObjectMapper.java
package com.tianshu.evp.orderService.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
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 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;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* @Author: wqf2021/04/26
* @Date: 2021/04/26
* @Description:
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
//时间序列化
builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
//针对于JDK新时间类。序列化时带有T的问题,自定义格式化字符串
JavaTimeModule javaTimeModule = new JavaTimeModule();
//LocalDateTime
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//LocalDate
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//LocalTime
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.modules(javaTimeModule);
//序列化将BigDecimal转String类型 BigDecimal数据返回到前端会出现精度丢失的问题
builder.featuresToEnable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
//Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
//未知字段不序列化
builder.failOnUnknownProperties(false);
//允许出现特殊字符和转义符
builder.featuresToEnable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
// 允许出现单引号
builder.featuresToEnable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
return builder.createXmlMapper(false).build();
}
}
使用配置文件的方式,解决了问题。
开发中,配置时间序列化转换,未知字段不序列化,null值不序列化(减少数据传输),差不多够用了,如果还有为null的数组要序列化为[]空数组的等业务需求,自己在配上就好了。
有问题,还请各位提出,大家一起进步!
最后提供另一位大佬的博链接:https://www.cnblogs.com/scar1et/articles/14134024.html
2021/4/30 内容补充
package com.tianshu.evp.appservice.common.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
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 com.tianshu.evp.commons.domain.excption.BusinessException;
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;
import java.io.IOException;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* @Author: wqf2021/04/26
* @Date: 2021/04/26
* @Description:
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
//时间序列化
builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
//针对于JDK新时间类。序列化时带有T的问题,自定义格式化字符串
JavaTimeModule javaTimeModule = new JavaTimeModule();
//LocalDateTime
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
//LocalDate
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
//LocalTime
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.modules(javaTimeModule);
//Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
//未知字段不序列化
builder.failOnUnknownProperties(false);
//允许出现特殊字符和转义符
//builder.featuresToEnable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
// 允许出现单引号
//builder.featuresToEnable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
return builder.createXmlMapper(false).build();
}
// @Bean
// public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
// return builder -> {
// builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer());
// builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
// };
// }
// /**
// * 序列化
// */
// public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
// @Override
// public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
// throws IOException {
// if (value != null) {
// long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
// gen.writeNumber(timestamp);
// }
// }
// }
/**
* LocalDateTime反序列化 时间格式匹配
*/
public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
throws IOException {
String value = p.getValueAsString();
if (value.length() == 19) {
//传入时间格式为 2020-01-01 00:00:00;
return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} else if (value.length() == 13) {
//传入时间为时间戳
return Instant.ofEpochMilli(p.getValueAsLong()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
} else {
throw new BusinessException("请传入正确的时间格式!");
}
}
}
/**
* LocalDate反序列化
*/
public static class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext deserializationContext)
throws IOException {
String value = p.getValueAsString();
//判断时间格式 2020-01-01
if (value.length() == 10) {
//传入时间格式为 2020-01-01 00:00:00;
return LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} else if (value.length() == 13) {
//传入时间为时间戳
return Instant.ofEpochMilli(p.getValueAsLong()).atZone(ZoneOffset.ofHours(8)).toLocalDate();
} else {
throw new BusinessException("请传入正确的时间格式!");
}
}
}
}
网友评论