restful

restful 大概: 

http://www.ruanyifeng.com/blog/2011/09/restful.html

http://www.ibm.com/developerworks/cn/web/wa-spring3webserv/index.html

过 REST 风格体系架构,请求和响应都是基于资源表示的传输来构建的。资源是通过全局 ID 来标识的,这些 ID 一般使用的是一个统一资源标识符(URI)。
客户端应用使用 HTTP 方法(如,GET、POST、PUT 或 DELETE)来操作一个或多个资源。
通常,GET 是用于获取或列出一个或多个资源,POST 用于创建,PUT 用于更新或替换,而 DELETE 则用于删除资源。

Spring REST 支持的主要特性包括:

  • 注释,如 @RequestMapping@PathVariable,支持资源标识和 URL 映射
  • ContentNegotiatingViewResolver 支持为不同的 MIME/内容类型使用不同的表示方式
  • 使用相似的编程模型无缝地整合到原始的 MVC 层

@RequestMapping(value = "/{username}", method = RequestMethod.GET, headers = {"Accept=text/xml, application/json"})
        public @ResponseBody
        Spitter getSpitter(@PathVariable String username) {
          return spitterService.getSpitter(username);
        }

ContentNegotiatingViewResolver 先检查url,后检查header accept

 

HTTP message converters :可以将method返回client需要的形式,

Assuming that the Jackson JSON library is in the appli- cation’s classpath, the object returned from the handler method will be given to the MappingJacksonHttpMessageConverter for conversion into a JSON representation to be returned to the client. On the other hand, if the request header indicates that the client prefers text/xml, then Jaxb2RootElementHttpMessageConverter will be tasked with producing an XML response to the client. 

 

 关于url 解析到view:   http://www.cnblogs.com/zhaoyang/archive/2012/01/07/2315428.html

 

@RequestBody:发送请求的data

@RequestMapping(value = "/{username}", method = RequestMethod.PUT,
                headers = "Content-Type=application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateSpitter(@PathVariable String username,
                      @RequestBody Spitter spitter) {
  spitterService.saveSpitter(spitter);

}

When the request arrives, Spring MVC will see that the updateSpitter() is able to handle the request. But the message arrives as an XML document, and this method asks for a Spitter object. In this case, the MappingJacksonHttpMessageConverter may be chosen to convert the JSON message into a Spitter object. For that to work, the following criteria must be met:

The request’s Content-Type header must be set to application/json.
The Jackson JSON library must be available on the application’s classpath. 

原文地址:https://www.cnblogs.com/zengyou/p/2799687.html