Springmvc on the principle of JSON and XML automatic conversion

Source: Internet
Author: User
Tags json

Objective

SPRINGMVC is one of the current mainstream web MVC frameworks.

If a classmate is not familiar with it, then please refer to its introductory blog:http://www.cnblogs.com/fangjian0423/p/springmvc-introduction.html

Phenomenon

The demo used in this article is based on Maven and continues to be written based on the example of an entry-level blog.

Let's look at the corresponding phenomenon first. The key configuration in our profile *-dispatcher.xml is as follows (other regular configuration files are not explained, refer to the entry blog mentioned in the beginning of this article):

(View configuration omitted)

<mvc:resources location= "/static/" mapping= "/static/**"/>
<mvc:annotation-driven/>
< Context:component-scan base-package= "Org.format.demo.controller"/>

The following dependencies are required in the POM (Spring dependencies and other dependencies are not displayed):

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId> jackson-core-asl</artifactid>
  <version>1.9.13</version>
</dependency>
< dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId> jackson-mapper-asl</artifactid>
  <version>1.9.13</version>
</dependency>

This dependency is the dependency of JSON serialization.

Ok. We add a method to the controller:

@RequestMapping ("/xmlorjson")
@ResponseBody public
map<string, object> Xmlorjson () {
    map< String, object> map = new hashmap<string, object> ();
    Map.put ("List", Employeeservice.list ());
    return map;
}

Direct Access Address:

We see a few lines of configuration. After using the @responsebody annotation, the object returned by controller is automatically converted to the corresponding JSON data, where it has to lament the power of SPRINGMVC.

We do not seem to see the specific configuration, the only thing to see is *-dispatcher.xml in a configuration:<mvc:annotation-driven/>. This configuration, in fact, causes the Java object to automatically convert to a JSON object.

So how does spring actually implement the automatic conversion of Java objects to JSON objects? Why convert to JSON data and what if you want to convert it to XML data?

SOURCE Analysis

The spring version used in this article is 4.0.2.

Before we explain <mvc:annotation-driven/> this configuration, let's take a look at the message conversion mechanism for the next spring. @ResponseBody This annotation is the use of the message conversion mechanism, which is eventually converted to JSON data via a JSON converter.

The Httpmessageconverter interface is the HTTP message translation interface that spring provides. For this knowledge, you can refer to the second link in "Resources", which is very clear.

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.