If the JSON converter is not used, the following error occurs when the return method of the Ajax request declares @responsebody
"Networkerror:406 not Acceptable-http://uc.com:8081/login/check"
The/login/check request mapping method is as follows
@RequestMapping ("/login/check") @ResponseBody public map<string, string> Logincheck ( HttpServletRequest req) { Mapnew hashmap<string, string>(); Rs.put ("Result", "success"); Rs.put ("message", "Login Successful"); return rs;}
Because the @responsebody declaration, the Jackson packet needs to be handled, the workaround is as follows
first, the introduction of the Jackson bag
Declare in Pom.xml to import the Jackson Pack
<Dependency> <groupId>Org.codehaus.jackson</groupId> <Artifactid>Jackson-mapper-asl</Artifactid> <version>1.9.13</version></Dependency><Dependency> <groupId>Org.codehaus.jackson</groupId> <Artifactid>Jackson-core-asl</Artifactid> <version>1.9.13</version></Dependency>
Second, the configuration Spring.xml
Add a bit of content to the spring configuration file
<!--Register the JSON converter and customize the date format conversion -<Mvc:annotation-driven> <mvc:message-converters> <Beanclass= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> < Propertyname= "Objectmapper"> <Beanclass= "Org.mastiff.tools.ToolJacksonDateFormat"></Bean> </ Property> </Bean> </mvc:message-converters></Mvc:annotation-driven>
If you do not need additional configuration (this is the date format processing), here you actually only need one row
< Mvc:annotation-driven />
The Org.mastiff.ToolJacksonDateFormat class is used for date format conversions, converting all dates to YYYY-MM-DD HH:mm:ss format, source code
PackageCom.ywlaker.tools;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.codehaus.jackson.JsonGenerator;Importorg.codehaus.jackson.JsonProcessingException;ImportOrg.codehaus.jackson.map.JsonSerializer;ImportOrg.codehaus.jackson.map.ObjectMapper;ImportOrg.codehaus.jackson.map.SerializerProvider;Importorg.codehaus.jackson.map.ser.CustomSerializerFactory;/*** @ Function Description: Jackson to JSON when formatted date display, global settings *@authorYang.wei * @time August 18, 2015 PM 2:43:14 *@version0.0.1*/ Public classTooljacksondateformatextendsObjectmapper { PublicTooljacksondateformat () {Customserializerfactory factory=Newcustomserializerfactory (); Factory.addgenericmapping (Date.class,NewJsonserializer<date>() {@Override Public voidSerialize (date date, Jsongenerator jsongenerator, Serializerprovider arg2)throwsIOException, jsonprocessingexception {simpledateformat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Jsongenerator.writestring (Sdf.format (date)); }}); This. Setserializerfactory (Factory); }}
Springmvc using the Jackson to JSON format