您的位置:首页 > 移动开发

Spring @RequestMapping的用法

2015-03-19 17:32 369 查看
Spring @RequestMapping用来处理请求映射的注解,可以用在类或者方法上;当被用在类上时,类中所有相应请求的方法都以该路径作为父路径。

RequestMapping注解有六个属性:

1、value:指定请求的实际地址,指定的地址可以是URI Template 模式。

2、method:指定请求的method类型,包括GET、POST、PUT、DELETE等。

3、consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, multipart/form-data。

4、produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。

5、params:指定request中必须包含某些参数值,才让该方法处理。
6、headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

示例:

@Controller
@RequestMapping("/api/sign")
public class SignApi {
@ResponseBody
@RequestMapping(value = "/sign", method = RequestMethod.POST, produces = "application/json;charset=UTF-8", consumes = "multipart/form-data")
public SignResult sign(HttpServletRequest request,
@RequestParam(value = "json") String json,
@ApiParam(name = "image") @RequestParam(value = "image") MultipartFile signature) {

......//代码
}
}

value的uri值为以下三类:

A) 可以指定为普通的具体值;

B)  可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);

C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions)

参考文档:
http://blog.csdn.net/walkerjong/article/details/7994326 http://docs.spring.io/spring/docs/4.2.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#mvc-ann-methods
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring RequestMapping