Using spring framework to return date in JSON format in Java Web program

Format date when returning JSON step 1: create customobjectmapper class

Step 2: configure as follows:

The effect is as follows: before formatting

After formatting

Advanced: returns the date format of JSON string when using @ ResponseBody. The date type attribute returns a long timestamp by default. How can I return a user-defined date format? Solution: at present, there are two ways to realize it: 1. Local modification (there are many online, but it is not recommended); Inherit Jackson's abstract class: jsonserializer < T >, and then add the annotation @ jsonserialize on the JavaBean property getter (). The code is as follows:

Usage:

2. Global modification (strongly recommended): the mappingjacksonhttpmessageconverter mainly returns JSON strings through objectmapper. Here, we inherit this class and register a jsonserializer < T >. Then inject the custom objectmapper into the configuration file. The code is as follows:

spring-servlet. Configuration in XML:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>