Springmvc using the Jackson to JSON format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.