内容简介:在springboot里,推荐我们通过我们来定义自己的WebMvcConfigurer,并且重写一个JSON输出的格式然后我们在返回对象之后,它的double,BigDecimal的属性将被格式化
在springboot里,推荐我们通过 WebMvcConfigurer
的派生类重写你的请求,你可以利用WebMvcConfigurer对http请求添加一些拦截器,addCorsMappings,addResourceHandlers,消息返回格式等等,需要注意的是,你需要使用 @EnableWebMvc
注解override springboot默认的方法;WebMvcConfigurer虽然是接口,但它的方法都是有default默认实现的。
public interface WebMvcConfigurer {
default void configurePathMatch(PathMatchConfigurer configurer) {
}
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
}
...
}
我们来定义自己的WebMvcConfigurer,并且重写一个JSON输出的格式
@Configuration
@EnableWebMvc //覆盖默认的配置
public class WebMvcConfigurerImpl implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
/**
* 序列换成Json时,将所有的Long变成String
* 因为js中得数字类型不能包括所有的java Long值
*/
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
// 所有的double类型返回保留三位小数
simpleModule.addSerializer(BigDecimal.class, new MoneySerialize());
// double保留两位小数
simpleModule.addSerializer(Double.class, new DoubleSerialize());
simpleModule.addSerializer(Double.TYPE, new DoubleSerialize());
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
}
/**
* money serializer.
*/
public class MoneySerialize extends JsonSerializer<Object> {
//修改要除的数据
final BigDecimal TEMP = BigDecimal.valueOf(1000000L);
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) {
BigDecimal bigDecimal = new BigDecimal(value.toString());
gen.writeNumber(bigDecimal.divide(TEMP, 4, RoundingMode.DOWN));
}
}
}
/**
* double serializer.
*/
public class DoubleSerialize extends JsonSerializer<Double> {
private DecimalFormat df = new DecimalFormat("##.00");
@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
if (value != null) {
gen.writeString(df.format(value));
}
}
}
然后我们在返回对象之后,它的double,BigDecimal的属性将被格式化
{"name":"zzl","email":null,"sex":null,"total":"5.00","totalMoney":0.0001}
以上所述就是小编给大家介绍的《springboot~重写json序列化方式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 能用js重写的都会被typescript重写
- 重写equals的详细说明
- SpringSession:请求与响应重写
- 什么时候要重写equals
- 如何重写object虚方法
- 重写Jekyll的Relate功能
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
极致:互联网时代的产品设计
戴维•罗斯 / 中信出版集团 / 2016-6 / 49.00元
在不远的未来,日常物品将能够迅速理解我们的需求,改善我们的生活,并随处可见。为了实现这一预期,我们需要能够发现用户使用产品的场景,找到用户高频刚需痛点的产品设计者。 站在下一个转型发展的悬崖上,我们看到技术将更具人性。随着物联网的发展,我们习以为常的数百件日常物品:汽车、钱包、手表、雨伞甚至垃圾桶,都将回应我们的需求,了解我们,学习为我们思考。最先出现的智能硬件为什么是智能手环、无人驾驶汽车......一起来看看 《极致:互联网时代的产品设计》 这本书的介绍吧!