Spring MVC实现Restful Web Service
2015-01-07 10:17
465 查看
一引言:
以前一说到Web Service大家肯定会联想到SOAP,现在提到Web Service大家马上联想到RESTful,因为RESTful Web Service已经深得人心,得到重用,相比笨重的SOAP越来越流行了,那么什么是RESTful Web Service?REST英文全称为Representational State Transfer,翻译为中文即表征状态转移,是一种软件架构风格,REST关键原则为:
为所有“事物”定义ID
将所有事物链接在一起
使用标准方法
资源多重表述
无状态通信
RESTful Web Service 是一个使用HTTP并遵循REST原则的Web服务。它从三个方面资源进行定义:
URI,比如:http://example.com/resources/。
Web Service接受与返回的互联网媒体类型,比如:JSON,XML等。
Web Service在该资源上所支持的一系列请求方法(比如:POST,GET,PUT或DELETE)。
二SpringMVC对RESTful Web Service的支持:
1.将URI和HTTP请求方法映射到JAVA处理方法,并将JAVA方法处理结果返回给HTTP请求者(对应资源定义I和III)。
@RequestMapping
这是最重要的一个注解,用于处理HTTP请求地址映射,可用于类或方法上,用于类上时表示类中的所有响应请求的方法都是以该地址作为父路径,在Spring中,一般一个Controller类处理一种资源,所以每个Controller类都会加@RequestMapping注解。
常用属性:
value:指定请求的地址
method:指定请求的method类型, GET、POST、PUT、DELETE等
params:指定request中必须包含某些参数值是,才让该方法处理
@PathVariable
映射URL路径里面的参数
2.将接收的数据(如JSON格式数据)转换为JAVA对象和将JAVA对象转换为请求格式(如XML)的数据(对应资源定义II)。
@RequestBody
用于读取Request请求的body数据,使用Bean配置中的 HttpMessageConverter 将数据转换为JAVA对象,再把对象绑定到 controller中方法的参数上。
@ResponseBody
用于将Controller中方法返回的对象,使用Bean配置中的 HttpMessageConverter 转换为指定格式数据,再写入到Response对象的body数据区。
HttpMessageConverter配置示例:
默认情况下Spring已经启用了很多HttpMessageConverter,只要将他们的实现在类路径下即可,无需再配置。
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)
Spring做得太贴心了,所以开发者简单配置就搞定RESTful WebService功能,然后就可专注于业务逻辑实现上。
以前一说到Web Service大家肯定会联想到SOAP,现在提到Web Service大家马上联想到RESTful,因为RESTful Web Service已经深得人心,得到重用,相比笨重的SOAP越来越流行了,那么什么是RESTful Web Service?REST英文全称为Representational State Transfer,翻译为中文即表征状态转移,是一种软件架构风格,REST关键原则为:
为所有“事物”定义ID
将所有事物链接在一起
使用标准方法
资源多重表述
无状态通信
RESTful Web Service 是一个使用HTTP并遵循REST原则的Web服务。它从三个方面资源进行定义:
URI,比如:http://example.com/resources/。
Web Service接受与返回的互联网媒体类型,比如:JSON,XML等。
Web Service在该资源上所支持的一系列请求方法(比如:POST,GET,PUT或DELETE)。
HTTP 请求方法在RESTful Web 服务中的典型应用 | ||||
资源 | GET | PUT | POST | DELETE |
一组资源的URI,比如http://example.com/resources/ | 列出 URI,以及该资源组中每个资源的详细信息(后者可选)。 | 使用给定的一组资源替换当前整组资源。 | 在本组资源中创建/追加一个新的资源。该操作往往返回新资源的URL。 | 删除整组资源。 |
单个资源的URI,比如http://example.com/resources/142 | 获取指定的资源的详细信息,格式可以自选一个合适的网络媒体类型(比如:XML、JSON等) | 替换/创建指定的资源。并将其追加到相应的资源组中。 | 把指定的资源当做一个资源组,并在其下创建/追加一个新的元素,使其隶属于当前资源。 | 删除指定的元素。 |
1.将URI和HTTP请求方法映射到JAVA处理方法,并将JAVA方法处理结果返回给HTTP请求者(对应资源定义I和III)。
@RequestMapping
这是最重要的一个注解,用于处理HTTP请求地址映射,可用于类或方法上,用于类上时表示类中的所有响应请求的方法都是以该地址作为父路径,在Spring中,一般一个Controller类处理一种资源,所以每个Controller类都会加@RequestMapping注解。
常用属性:
value:指定请求的地址
method:指定请求的method类型, GET、POST、PUT、DELETE等
params:指定request中必须包含某些参数值是,才让该方法处理
1 2 3 4 5 6 7 8 9 10 11 | @Controller @RequestMapping(value = "/contact") public class ContactController { final Logger logger = LoggerFactory.getLogger(ContactController.class); @Autowired private ContactService contactService; @RequestMapping(value = "/listdata", method = RequestMethod.GET) @ResponseBody public Contacts listData() { return new Contacts(contactService.findAll()); } |
映射URL路径里面的参数
1 2 3 4 5 | @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody public Contact findContactById(@PathVariable Long id) { return contactService.findById(id); } |
@RequestBody
用于读取Request请求的body数据,使用Bean配置中的 HttpMessageConverter 将数据转换为JAVA对象,再把对象绑定到 controller中方法的参数上。
1 2 3 4 5 6 7 8 | @RequestMapping(value = "/", method = RequestMethod.POST) @ResponseBody public Contact create(@RequestBody Contact contact) { logger.info("Creating contact: " + contact); contactService.save(contact); logger.info("Contact created successfully with info: " + contact); return contact; } |
用于将Controller中方法返回的对象,使用Bean配置中的 HttpMessageConverter 转换为指定格式数据,再写入到Response对象的body数据区。
1 2 3 4 5 | @RequestMapping(value = "/listdata", method = RequestMethod.GET) @ResponseBody public Contacts listData() { return new Contacts(contactService.findAll()); } |
1 2 3 4 5 6 7 8 9 10 | <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <property name="marshaller" ref="castorMarshaller" /> <property name="unmarshaller" ref="castorMarshaller" /> </bean> </mvc:message-converters> </mvc:annotation-driven> |
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)
Spring做得太贴心了,所以开发者简单配置就搞定RESTful WebService功能,然后就可专注于业务逻辑实现上。
相关文章推荐
- Struts、Ajax、Restful、Web Service 狂想曲框架实现篇(三)
- 写在WCF实现RESTFul Web Service之前(二):REST基本概念
- Web Service笔记(六):基于Restful服务端的客户端实现(WebClient 和 HttpURLConnection)
- Web——WCF实现RESTFul Web Service
- RESTful Web Service - http身份登录认证的实现和使用(一)
- Struts、Ajax、Restful、Web Service 狂想曲框架实现篇(一)
- 使用Spring MVC实现RESTful接口(详细-初级)
- spring mvc实现Restful返回json格式数据
- 构建RESTful Web Service - 验证的实现和使用(HTTP 基本认证)
- Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service
- 写在WCF实现RESTFul Web Service之前(三):RESTful WebService VS SOAP WebService
- [Jersey]使用jersey实现RESTful web service 之 web.xml配置问题
- Django实战(15):Django实现RESTful web service
- spring mvc实现Restful返回xml格式数据
- WCF实现RESTFul Web Service(二):REST基本概念
- 写在WCF实现RESTFul Web Service之前(一):JSON基本概念
- (转)Django ====> 实战学习篇十一 Django实现RESTFul web service,Django+jquery,ajax
- Spring mvc实现RESTful Web 服务Demo
- WCF实现RESTFul Web Service
- Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service