(i) Output JSON data
JSON output can be done using JACKSON-MAPPER-ASL in Springmvc, with several configurations:
1. Use Mvc:annotation-driven2. Add Jackson-mapper-asl in Dependency management
1 <dependency>2 <groupid>org.codehaus.jackson</groupid>3 <artifactId> Jackson-mapper-asl</artifactid>4 <version>${jackson.version}</version>5 </dependency >
3. Using Annotations @responsebody
Mvc:annotation-driven The JSON converter is loaded by default, we can use annotation @responsebody to return JSON data after we add the above dependency package, for example:
1 @RequestMapping ("JSON") 2 @ResponseBody3 public list<user> userlist (Modelmap modelmap) {4 userexample example = new Userexample (); 5 Example.createcriteria (). Andusernameisnotnull (); 6 List<user > users = usermapper.selectbyexample (example); 7 return users;8 }
(ii) Format the date format of the JSON output
Although JSON is output, JSON has a long value for the date type, like when the page is taken out as a foreign date format, we need to add a format conversion to convert the date format to the desired format: YYYY-MM-DD.
1. Using @jsonformat (pattern= "Yyyy-mm-dd HH:mm:ss", timezone = "gmt+8")
Add @jsonformat (pattern= "Yyyy-mm-dd HH:mm:ss", timezone = "gmt+8") above the getter method of the entity class to format the date of the JSON.
The first time I try to always fail, and then add a complete dependency package after success, you need to add several dependencies as follows:
1 <!--JSON data--2 <dependency> 3 <groupId>org.codehaus.jackson</groupId> 4 <artifactId>jackson-mapper-asl</artifactId> 5 <version>${jackson.version}</ve Rsion> 6 </dependency> 7 <dependency> 8 <groupid>com.fasterxml.jackson.co Re</groupid> 9 <artifactid>jackson-core</artifactid>10 <version>${jackson. Core.version}</version>11 </dependency>12 <dependency>13 <groupid>com. Fasterxml.jackson.core</groupid>14 <artifactid>jackson-databind</artifactid>15 & Lt;version>${jackson.core.version}</version>16 </dependency>17 <properties>20 & Lt;jackson.version>1.9.13</jackson.version>21 <jackson.core.version>2.4.2</ Jackson.core.version>22 </properties>
The advantage is simple and convenient, and the disadvantage is that it needs to be added on the getter method for each desired property. Macro look more cumbersome, but the actual development of a line of code, the only bad thing is that MyBatis automatically generated entity class will be overwritten.
2. Inheriting objectmapper to implement the return JSON string
Reference: http://aokunsang.iteye.com/blog/1878985
Although the above method is simple and convenient, but the disadvantage is also obvious, the automatic generation of code will overwrite the entity class, and each date attribute to be added manually, the actual date attribute is a universal necessity. Therefore, the large can be processed globally, in a uniform format. In this case, the date and timestamp in the database will be converted to the Date object by MyBatis. The format specification for birthdays to days and minutes to seconds allows the display layer to be processed. Unified into yyyy-mm-dd HH:mm:ss
The Mappingjacksonhttpmessageconverter is primarily implemented by Objectmapper to return a JSON string. Here we inherit the class, registering a jsonserializer<t>. The custom objectmapper is then injected into the configuration file.
2.1 Writing Subclass inheritance Objectmapper
1 package com.demo.common.util.converter; 2 3 Import Org.codehaus.jackson.JsonGenerator; 4 Import org.codehaus.jackson.JsonProcessingException; 5 Import Org.codehaus.jackson.map.JsonSerializer; 6 Import Org.codehaus.jackson.map.ObjectMapper; 7 Import Org.codehaus.jackson.map.SerializerProvider; 8 Import Org.codehaus.jackson.map.ser.CustomSerializerFactory; 9 Import java.io.ioexception;11 Import java.text.simpledateformat;12 import java.util.date;13 14/**15 * Resolve Date type return JSO n format for custom format * Created by Administrator on 2016/2/14.17 */18 public class Customjsondateconverter extends Objectmapper {1 9 public Customjsondateconverter () {Customserializerfactory factory = new Customserializerfactory (); 21 Factory.addgenericmapping (Date.class, New jsonserializer<date> () {@Override23 public vo ID Serialize (Date value,24 jsongenerator jsongenerator,25 Serializerprovider ProvidER) throws IOException {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd H H:mm:ss "); Jsongenerator.writestring (Sdf.format (value));}30}); this.se Tserializerfactory (factory); 32}33}
2.2 Configuring Spring Files
1 <mvc:annotation-driven>2 <mvc:message-converters>3 <bean class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ">4 <property name=" Objectmapper "ref=" Customobjectmapper "></property>5 </bean>6 </mvc:message-converters >7 </mvc:annotation-driven>8 <bean id= "Customobjectmapper" class= " Com.demo.common.util.converter.CustomJsonDateConverter "></bean>
2.3 Display layer self-determining date type length
This configuration cannot be used in conjunction with the previous @jsonformat. Date and DateTime, as well as timestamp, are all formatted as a result of a globally uniform format, and if date fields such as birthdays need to be streamlined, they can only be cropped at the display layer.
3. Use the built-in date formatting tool
The same is true for the global setting of the date format of the JSON response, but this method can coexist with @jsonformat, which means that a format can be set globally, with annotation settings for specific requirements.
3.1 Configuring Spring Files
<mvc:annotation-driven> <!--processing responsebody inside Date type-- <mvc:message-converters> <bean class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > < Property name= "Objectmapper" > <bean class= "Com.fasterxml.jackson.databind.ObjectMapper" > < Property name= "DateFormat" > <bean class= "Java.text.SimpleDateFormat" > <constructor-arg type= " Java.lang.String "value=" Yyyy-mm-dd HH:mm:ss "/> </bean> </property> </bean> </property> </bean> </mvc:message-converters> </MVC: Annotation-driven>
3.2 Configuring a specific date
@JsonFormat (pattern= "Yyyy-mm-dd", timezone = "gmt+8") public Date Getbirth () { return birth; }
springmvc--json--return JSON for date format issues