一、简介
近几年,以信息为中心的表述行状态转移(Representational State Transfer, REST)已成为替换传统SOAP Web服务端流行方案。Spring对REST的支持是构建在Spring MVC之上的,所以需要先了解Spring MVC的知识。
二、构建Spring REST步骤
1、配置前端控制器
1 <servlet> 2 <servlet-name>dispatcher</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <load-on-startup>1</load-on-startup> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name>dispatcher</servlet-name> 8 <url-pattern>/</url-pattern> 9 </servlet-mapping>
2、配置组件扫描
<context:component-scan base-package="com.cnblogs.javalouvre.mvc" />
扫描到有@RestController、@Controller、@Compoment、@RestController、@Repository等注解的类,则把这些类注册为Bean。
3、配置注解映射支持
<mvc:annotation-driven />
4、请求消息处理
1 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 2 <property name="messageConverters"> 3 <list> 4 <ref bean="mappingJackson2HttpMessageConverter"/> 5 <ref bean="marshallingHttpMessageConverter"/> 6 </list> 7 </property> 8 </bean> 9 10 <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 11 <property name="supportedMediaTypes" value="application/json" /> 12 </bean> 13 14 <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 15 <property name="classesToBeBound"> 16 <list> 17 <value>com.cnblogs.javalouvre.model.Result</value> 18 </list> 19 </property> 20 </bean> 21 <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> 22 <property name="marshaller" ref="jaxb2Marshaller"/> 23 <property name="supportedMediaTypes" value="application/xml" /> 24 </bean>
5、定义控制器
1 package com.cnblogs.javalouvre.mvc; 2 3 import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; 4 import static org.springframework.http.MediaType.APPLICATION_XML_VALUE; 5 import static org.springframework.web.bind.annotation.RequestMethod.GET; 6 7 import org.springframework.web.bind.annotation.PathVariable; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.ResponseBody; 10 import org.springframework.web.bind.annotation.RestController; 11 12 import com.cnblogs.javalouvre.model.Account; 13 import com.cnblogs.javalouvre.model.Result; 14 15 @RestController 16 @RequestMapping(value = "/account") 17 public class AccountController { 18 19 @ResponseBody 20 @RequestMapping(value = "/{id}", method = GET, produces = { APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE }) 21 public Result getAccount(@PathVariable(value = "id") Integer id) { 22 return new Result("1", "成功", new Account("211111111111111111", "啦啦啦。。。")); 23 } 24 }
6、消息封装POJO
1 package com.cnblogs.javalouvre.model; 2 3 import javax.xml.bind.annotation.XmlAccessType; 4 import javax.xml.bind.annotation.XmlAccessorType; 5 import javax.xml.bind.annotation.XmlElement; 6 import javax.xml.bind.annotation.XmlRootElement; 7 8 @XmlRootElement 9 @XmlAccessorType(XmlAccessType.FIELD) 10 public class Result implements java.io.Serializable { 11 12 private static final long serialVersionUID = -6254553465205204221L; 13 @XmlElement 14 private String code; 15 @XmlElement 16 private String message; 17 private Account account; 18 19 public Result() { 20 } 21 22 public Result(String code, String message, Account account) { 23 this.code = code; 24 this.message = message; 25 this.account = account; 26 } 27 28 // GETTER/SETTER 29 30 }
1 package com.cnblogs.javalouvre.model; 2 3 import java.io.Serializable; 4 5 import javax.xml.bind.annotation.XmlAccessType; 6 import javax.xml.bind.annotation.XmlAccessorType; 7 import javax.xml.bind.annotation.XmlElement; 8 import javax.xml.bind.annotation.XmlRootElement; 9 10 @XmlRootElement 11 @XmlAccessorType(XmlAccessType.FIELD) 12 public class Account implements Serializable { 13 14 private static final long serialVersionUID = -4282266548648755231L; 15 16 @XmlElement 17 private String oid; 18 19 @XmlElement 20 private String name; 21 22 public Account() { 23 } 24 25 public Account(String oid, String name) { 26 this.oid = oid; 27 this.name = name; 28 } 29 30 // GETTER/SETTER 31 32 }
7、测试
使用 Firefox RESTClient 测试,设置Accept为application/json,则返回JSON格式数据,设置Accept为application/xml,则返回XML格式数据,如下:
1 {"code":"1","message":"成功","account":{"oid":"211111111111111111","name":"啦啦啦。。。"}}
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><result><code>1</code><message>成功</message><account><oid>211111111111111111</oid><name>啦啦啦。。。</name></account></result>