SPRING-MVC RESTful Web Services

Source: Internet
Author: User

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

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.