关于SPRingMVC中的@ResquestMapping,@ResponseBody,@PathVariable,@ResquestParam三个注解的用法总是记不住,今天总结一下。
一、@ResquestMapping
先来说一说@ResquestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
例如下面的代码:
@Controller//此处是作为类上的请求地址映射@RequestMapping("/hello")public class HelloMvcController { //此处是作为方法上的地址映射 @RequestMapping("/mvc") //localhost:8080/hello/mvc public String helloMvc(){ return "home"; }}@RequestMapping的属性(分为三大类):
1、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
当然,我们最常用的参数应该就是value和method。例如:
//这里的value + method,value为访问方法的路径,method是该方法的请求方式是get @RequestMapping(value = "/list", method = RequestMethod.GET) public String list(Model model) { // 获取列表页 List<Seckill> list = seckillService.getSeckillList(); model.addAttribute("list", list); // list.jsp + model = ModelAndView return "list"; // WEB-INF/jsp/"list".jsp }还有一种用法是Produces属性:如下: 在springMVC controller中返回json数据出现乱码问题,因为没有进行编码,只需要简单的注解就可以了
// @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" }) @ResponseBody // 返回数据类型封装成json二、@ResponseBody
作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。 使用时机: 返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
@ResponseBody // 返回数据类型封装成json public SeckillResult<Exposer> exposer(@PathVariable Long seckillId) { SeckillResult<Exposer> result; try { Exposer exposer = seckillService.exportSeckillUrl(seckillId); result = new SeckillResult<Exposer>(true, exposer); } catch (Exception e) { logger.error(e.getMessage(), e); result = new SeckillResult<Exposer>(false, e.getMessage()); } return result; }在使用
<mvc:annotation-driven />标签配置时,默认配置了RequestMappingHandlerAdapter
ResourceHttpMessageConverter:负责读取资源文件和写出资源文件数据;
FormHttpMessageConverter: 负责读取form提交的数据(能读取的数据格式为 application/x-www-form-urlencoded,不能读取multipart/form-data格式数据);负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;
MappingJacksonHttpMessageConverter: 负责读取和写入json格式的数据;
SouceHttpMessageConverter: 负责读取和写入 xml 中javax.xml.transform.Source定义的数据; Jaxb2RootElementHttpMessageConverter: 负责读取和写入xml 标签格式的数据;
AtomFeedHttpMessageConverter: 负责读取和写入Atom格式的数据; rssChannelHttpMessageConverter: 负责读取和写入RSS格式的数据;
当使用@RequestBody和@ResponseBody注解时,RequestMappingHandlerAdapter就使用它们来进行读取或者写入相应格式的数据。
三、@PathVariable
当使用@RequestMapping URI template 样式映射时, 即 /{seckillId}/detail, 这时的seckillId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
@RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model) { return "detail"; }@RequestMapping(value = “/{seckillId}/detail”, method = RequestMethod.GET)中的{seckillId}与@PathVariable Long seckillId 一 一 对应,按名匹配, 这是restful式风格。 如果映射名称有所不一,可以参考如下方式:
@RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long Id, Model model) { return "detail"; }GET模式下,这里使用了@PathVariable绑定输入参数,非常适合Restful风格。因为隐藏了参数与路径的关系,可以提升网站的安全性,静态化页面,降低恶意攻击风险。
POST模式下,使用@RequestBody绑定请求对象,Spring会帮你进行协议转换,将Json、Xml协议转换成你需要的对象。
四、@ResquestParam
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter(“xx”),另外一种是用注解@RequestParam直接获取。 A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String–> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;
C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定; – value:参数名
– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常
@RequestMapping(value = "/testRequestParam", method = RequestMethod.GET) public String test(@RequestParam String inputStr){ System.out.println(inputStr); return "index"; }参考博客:
http://blog.csdn.net/pnoter/article/details/49680789
http://blog.csdn.net/kobejayandy/article/details/12690041
http://snowolf.iteye.com/blog/1628861
http://blog.csdn.net/u010127245/article/details/51774074
http://825635381.iteye.com/blog/2196911
新闻热点
疑难解答