spring-mvc入门:
http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html
1、@RequestBody && @ResponseBody
http://www.jianshu.com/p/7097fea8ce3f
http://snowolf.iteye.com/blog/1628861
应用场景:
1、适用于非键值对的情形,content-Type是application/json、application/xml等,但不是application/x-www-form-urlencoded;
2、用于简化从request中读取数据和向response写入数据
3、需要配合HttpMessageConverter来使用,spring-config.xml里需要添加注解<mvc:annotation-driven />,
RequestBody依据request.hader的content-Type来选择;ResponseBody依据request.header.accept值来选择。
4、HttpMessageConverter配置在mvc-config里,即配置一个类型是org.springframework.web.servlet.mvc.method.annotation.
RequestMappingHandlerAdapter的bean,指定属性messageConverters。
5、RequestBody和ResponseBody可以不同时使用.
6、Spring自带的、常用HttpMessageConverter有:StringHttpMessageConverter、ByteArrayHttpMessageConverter、ObjectToStringHttpMessageConverter、ResourceHttpMessageConverter
7、仅限于POST方式
2、@RequestParam
http://blog.csdn.net/walkerjong/article/details/7946109
应用场景:
1、适用于键值对的情形,即content-Type是application/x-www-form-urlencoded;
2、使用方式如下,GET、POST均可以:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam(“parameterName”) int petId, ModelMap model) {
….
}
3、@RequestHeader && @CookieValue
http://blog.csdn.net/walkerjong/article/details/7946109
应用场景:
1、可以把Request请求header部分的值(cookie中值)绑定到方法的参数上。
2、使用方式如下,GET、POST均可:
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding, @CookieValue("JSESSIONID") String cookie,
@RequestHeader("Keep-Alive") long keepAlive) {
...
}
4、@PathVariable
http://blog.csdn.net/walkerjong/article/details/7946109
应用场景:
1、绑定url路径值到方法的参数上,POST、GET均可。
2、使用方式:
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}
5、@RequestMapping
http://blog.csdn.net/kobejayandy/article/details/12690041
1、@RequestMapping("/owners/{ownerId}”)等效于@RequestMapping(path="/owners/{ownerId}")
2、@RequestMapping("/owners”, params={"id=215","name!=abc”}, headers={“Host=localhost:8088”,”provider=1”},
consumes = {"text/plain", "application/json”}, produces = {"text/plain", "application/*"})
表示:urlPath为/owners
&& url参数里包含id=215&name!=abc
&& headers里包含Host=localhost:8080 & provider=1
&& headers里的Content-Type为text/plain 或者application/json
&& headers里的Accept为text/plain 或者 application/*
那么,执行当前RequestMapping指定的方法。
3、method若不指定,那么表示GET和POST均可。