天天看点

Spring Web MVC实现Restful Web Service

一引言:

以前一说到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等)

替换/创建指定的资源。并将其追加到相应的资源组中。

把指定的资源当做一个资源组,并在其下创建/追加一个新的元素,使其隶属于当前资源。

删除指定的元素。

二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中必须包含某些参数值是,才让该方法处理

1

2

3

4

5

6

7

8

9

10

11

<code>@Controller</code>

<code>@RequestMapping</code><code>(value = </code><code>"/contact"</code><code>)</code>

<code>public</code> <code>class</code> <code>ContactController {</code>

<code>    </code><code>final</code> <code>Logger logger = LoggerFactory.getLogger(ContactController.</code><code>class</code><code>);</code>

<code>    </code><code>@Autowired</code>

<code>    </code><code>private</code> <code>ContactService contactService;</code>

<code>    </code><code>@RequestMapping</code><code>(value = </code><code>"/listdata"</code><code>, method = RequestMethod.GET)</code>

<code>    </code><code>@ResponseBody</code>

<code>    </code><code>public</code> <code>Contacts listData() {</code>

<code>        </code><code>return</code> <code>new</code> <code>Contacts(contactService.findAll());</code>

<code>    </code><code>}</code>

@PathVariable

映射URL路径里面的参数

<code>@RequestMapping</code><code>(value = </code><code>"/{id}"</code><code>, method = RequestMethod.GET)</code>

<code>    </code><code>public</code> <code>Contact findContactById(</code><code>@PathVariable</code> <code>Long id) {</code>

<code>        </code><code>return</code> <code>contactService.findById(id);</code>

2.将接收的数据(如JSON格式数据)转换为JAVA对象和将JAVA对象转换为请求格式(如XML)的数据(对应资源定义II)。

@RequestBody

用于读取Request请求的body数据,使用Bean配置中的 HttpMessageConverter 将数据转换为JAVA对象,再把对象绑定到 controller中方法的参数上。

<code>@RequestMapping</code><code>(value = </code><code>"/"</code><code>, method = RequestMethod.POST)</code>

<code>    </code><code>public</code> <code>Contact create(</code><code>@RequestBody</code> <code>Contact contact) {</code>

<code>        </code><code>logger.info(</code><code>"Creating contact: "</code> <code>+ contact);</code>

<code>        </code><code>contactService.save(contact);</code>

<code>        </code><code>logger.info(</code><code>"Contact created successfully with info: "</code> <code>+ contact);</code>

<code>        </code><code>return</code> <code>contact;</code>

@ResponseBody

用于将Controller中方法返回的对象,使用Bean配置中的 HttpMessageConverter 转换为指定格式数据,再写入到Response对象的body数据区。

<code>@RequestMapping</code><code>(value = </code><code>"/listdata"</code><code>, method = RequestMethod.GET)</code>

HttpMessageConverter配置示例:

<code>&lt;</code><code>mvc:annotation-driven</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>mvc:message-converters</code><code>&gt;</code>

<code>            </code><code>&lt;</code><code>bean</code> <code>class</code><code>=</code><code>"org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"</code><code>/&gt;</code>

<code>            </code><code>&lt;</code><code>bean</code>

<code>                </code><code>class</code><code>=</code><code>"org.springframework.http.converter.xml.MarshallingHttpMessageConverter"</code><code>&gt;</code>

<code>                </code><code>&lt;</code><code>property</code> <code>name</code><code>=</code><code>"marshaller"</code> <code>ref</code><code>=</code><code>"castorMarshaller"</code> <code>/&gt;</code>

<code>                </code><code>&lt;</code><code>property</code> <code>name</code><code>=</code><code>"unmarshaller"</code> <code>ref</code><code>=</code><code>"castorMarshaller"</code> <code>/&gt;</code>

<code>            </code><code>&lt;/</code><code>bean</code><code>&gt;</code>

<code>        </code><code>&lt;/</code><code>mvc:message-converters</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>mvc:annotation-driven</code><code>&gt;</code>

默认情况下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&lt;String, String&gt;.

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功能,然后就可专注于业务逻辑实现上。

<a href="http://down.51cto.com/data/2364035" target="_blank">附件:http://down.51cto.com/data/2364035</a>

     本文转自sarchitect 51CTO博客,原文链接:http://blog.51cto.com/stevex/1355154,如需转载请自行联系原作者