JSON

解决SpringMVC 返回Java8 时间JSON数据的格式化问题处理

字号+ 作者:H5之家 来源:H5之家 2017-03-07 10:23 我要评论( )

解决SpringMVC 返回Java8 时间JSON数据的格式化问题处理_java_HTML5中文学习网,是中国最大的HTML5中文门户,为广大HTML5爱好者提供各种HTML5资料,包括HTML5网

有时在Spring MVC中返回JSON格式的response的时候会使用@ResponseBody注解,不过在处理java8中时间的时候会很麻烦,一般我们使用的HTTPMessageConverter是MappingJackson2HttpMessageConverter,它默认返回的时间格式是这种:

继承ObjectMapper来实现返回json字符串。MappingJackson2HttpMessageConverter主要通过ObjectMapper来实现返回json字符串。这里我们编写一个JsonUtil类,获取ObjectMapper以针对java8中新的日期时间API,注册相应的JsonSerializer<T>。4JaHTML5中文学习网 - HTML5先行者学习网

/** * json处理工具类 * * */@Componentpublic class JsonUtil { private static final ObjectMapper mapper; public ObjectMapper getMapper() { return mapper; } static { mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(LocalDate.class, new LocalDateSerializer()); module.addSerializer(LocalTime.class, new LocalTimeSerializer()); module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); mapper.registerModule(module); } public static String toJson(Object obj) { try { return mapper.writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException("转换json字符失败!"); } } public <T> T toObject(String json, Class<T> clazz) { try { return mapper.readValue(json, clazz); } catch (IOException e) { throw new RuntimeException("将json字符转换为对象时失败!"); } }}class LocalDateSerializer extends JsonSerializer<LocalDate> { private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); @Override public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(dateFormatter.format(value)); }}class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Override public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(dateTimeFormatter.format(value)); }}class LocalTimeSerializer extends JsonSerializer<LocalTime> { private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); @Override public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(timeFormatter.format(value)); }}

然后在springmvc的配置文件中,再将<mvc:annotation-driven/>改为以下配置,配置一个新的json转换器,将它的ObjectMapper对象设置为JsonUtil中的objectMapper对象,此转换器比spring内置的json转换器优先级更高,所以与json有关的转换,spring会优先使用它。4JaHTML5中文学习网 - HTML5先行者学习网
4JaHTML5中文学习网 - HTML5先行者学习网

<mvc:annotation-driven> <mvc:message-converters> <bean> <property value="#{jsonUtil.mapper}" /> <property> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>

然后java8中的几种日期和时间类型就可以正常友好的显示了。优点是全局统一管理日期和时间等类型,缺点对pojo中的某个域做特殊处理。4JaHTML5中文学习网 - HTML5先行者学习网
4JaHTML5中文学习网 - HTML5先行者学习网

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。4JaHTML5中文学习网 - HTML5先行者学习网

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评