spring boot:从零开始搭建一个项目 - day9 返回String带双引号以及序列化LocalDateTime问题解决
问题一:返回String带双引号
引出:token返回前台时两边存在双引号。
原因:字符串被json转换器转成json,前后就多出了双引号。
解决方案:引入Fastjson,在配置文件中加入如下代码
@Configuration
public class WebConfig {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverterExtension();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
public class FastJsonHttpMessageConverterExtension extends FastJsonHttpMessageConverter {
FastJsonHttpMessageConverterExtension() {
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
mediaTypes.add(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
setSupportedMediaTypes(mediaTypes);
}
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converters.add(converter);
}
}
问题二:返回LocalDateTime时格式是个数组
引出:更新数据的时候报错,查看前台接收到的是一个数组。
原因:LocalDateTime反序列化失败,需要对LocalDateTime。
解决方案:扩充fastJsonHttpMessageConverters()
方法,序列化LocalDateTime
//解决返回字符串存在""
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverterExtension();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullBooleanAsFalse, //boolean null返回false
SerializerFeature.WriteNullStringAsEmpty, //字符串null返回空字符串
SerializerFeature.WriteNullListAsEmpty, //空字段保留
SerializerFeature.WriteMapNullValue);
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//解决Long转json精度丢失的问题
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
serializeConfig.put(BigDecimal.class,ToStringSerializer.instance);
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
//序列化LocalDateTime
serializeConfig.put(LocalDateTime.class, LocalDateTimeSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter, new StringHttpMessageConverter(Charset.forName("UTF-8")));
}
LocalDateTimeSerializer
模仿 ToStringSerializer
CV一下
public class LocalDateTimeSerializer implements ObjectSerializer {
public static final LocalDateTimeSerializer instance = new LocalDateTimeSerializer();
private static final String defaultPattern = "yyyy-MM-dd HH:mm:ss";
public LocalDateTimeSerializer() {
}
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull();
} else {
LocalDateTime result = (LocalDateTime) object;
out.writeString(result.format(DateTimeFormatter.ofPattern(defaultPattern)));
}
}
}
总结:Fastjson真好用!