SPRINGMVC returns XML or JSON-formatted data

Source: Internet
Author: User

SPRINGMVC web.xml Configuration I will not say ...

First you need to add the JSON jar package:
Jackson-mapper-asl-*.jar
Jackson-core-asl-*.jar

In the Applicationcontext-servlet.xml file configuration, add the following registration default message converter:

<mvc:annotation-driven/>

Controller

    @RequestMapping (method=requestmethod.get,value= "/rest")
    @ResponseBody the public
    User rest Test ( HttpServletRequest request, Uservo vo) {
        User user = new user ();
        User.setname ("YP");
        User.setpassword ("TC");
        return user;
    

User class:

@XmlRootElement public
class User implements serializable{

    private Integer ID;

    private String name;

    private String password;

    @XmlElement public
    Integer getId () {return
        ID;
    }

    public void SetId (Integer id) {
        this.id = ID;
    }

    @XmlElement public
    String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    @XmlElement public
    String GetPassword () {return
        password;
    }

    public void SetPassword (String password) {
        this.password = password;
    }
}

Note that the user class above must be annotated with a @xmlrootelement, or it will be returned by JSON.

Then enter the URL address directly in the browser to access the line.

Add < Mvc:annotation-driven/>debug to the spring configuration file above you will find the following figure:

Inside the default registered XML converter and JSON converter ...

When you add a @xmlrootelement to the user class, you return XML-formatted data using an XML converter without returning the JSON-formatted data with @xmlrootelement.

Of course, the data converted into what format is specified by the Accept property of the request header.
The following request returns data in XML format:

   function Jsontest () {
        $.get ("rest.htm", "", function (res) {
                alert (res);
            Console.log (res);
            }, "xml");
    

The following request returns the JSON-formatted data:

   function Jsontest () {
        $.get ("rest.htm", "", function (res) {
                alert (res);
            Console.log (res);
            }, "json");
    

If you want to see a specific SPRINGMVC data conversion process here is a link: http://www.chawenti.com/articles/23596.html

Of course, there are other configuration methods, such as not using the default registered converter, can be configured in < mvc:annotation-driven>

<mvc:annotation-driven>
        <mvc:message-converters register-defaults= "true" >
            <bean class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ">
                    <property name=" Supportedmediatypes ">
                        <list>
                            <value>application/json</value>
                            <value> text/json</value>
                        </list>
                    </property>
            </bean>
        </MVC: Message-converters>
    </mvc:annotation-driven>

Or

<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
        < Property name= "Messageconverters" >
            <list>
                <bean
                    class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ">
                    <property name=" Supportedmediatypes ">
                        <list>
                            <value>application/json</value>
                            <value> text/json</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

The two ways are almost ... I also read this from the Internet. who plays on the island, can add me to the team: A.

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.