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.