Spring can easily integrate a variety of restful styles of web-service, but in fact SPRING-MVC has provided this support.
You can generate XML, JSON, text, and other formats as needed.
Generating XML relies on JAXB, mainly @xmlrootelement, @XmlAttribute, @XmlElement and other annotations, JDK1.6 began with JAXB.
Generating JSON requires Jackson, who relies on Jackson-mapper-asl-xxx.jar, Jackson-core-asl-xxx.jar. MAVEN relies on the following, JACKSON-CORE-ASL will automatically join.
<!--Jackson--><dependency> <groupId>org.codehaus.jackson</groupId> <artifactId> Jackson-mapper-asl</artifactid> <version>1.7.9</version></dependency>
The following is a simple controller. produces in @RequestMapping annotations specifies the format of the output, XML, JSON, etc.
package com.spring.demo.web.controller;import javax.annotation.postconstruct;import Org.slf4j.logger;import org.slf4j.loggerfactory;import org.springframework.http.httpstatus;import org.springframework.http.MediaType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody;import org.springframework.web.bind.annotation.responsestatus;import com.spring.demo.core.entity.member;/** * rest demo * * @author sean * */@ Controller@requestmapping ("/rest") public class restcontroller { private logger logger = loggerfactory.getlogger (GetClass ()); private Member member; @PostConstruct Public void postconstrucT () { member = new member (); member.setemail ("[email protected]"); member.setname ("Zhang San"); member.setstreet (" South Mt. District, 55th Tianhe Street, Pak Shek Chau, member.setzip ("518025"); } /** * Output xml * * @return */ @RequestMapping (value = "/xml", produces = { mediatype.application_xml_value }) @ResponseBody public member xml () { logger.debug ("Rest xml"); return member; } /** * Output json * * @ return */ @RequestMapping (value = "/json", produces = { mediatype.application_json_value }) @ResponseBody public member json () { Logger.debug ("Rest json"); return member; } /** * Update Operations, You only need to return to the status of success or not. * * @return */ @RequestMapping (value= "/updateandreturnok") @ responsebody public string updateandreturnok () { Logger.debug ("Rest updateandreturnok"); return "OK"; } /** * Update operation, nothing is returned. */ @RequestMapping (value= "/update") @ResponseStatus (value=httpstatus.no_content) @ResponseBody public void update () { logger.debug ("Rest update and return no content "); }}
Source Reference Restcontroller, Member
SPRING-MVC RESTful Web Services