@RequestMapping的参数和用法

@RequestMapping的参数和用法
一、简介
二、属性
1、value
1)说明
2)示例
a.普通的具体值
b.含有某变量的一类值(URI Template Patterns with Path Variables)
c.含正则表达式的一类值( URI Template Patterns with Regular Expressions)
2、method
1)说明
2)示例
3、consumes
1)说明
2)示例
4、produces
1)说明
2)示例
5、params
1)说明
2)示例
6、headers
1)说明
2)示例
三、@RequestMapping 快捷方式
一、简介
RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。

用于类上:表示类中的所有响应请求的方法都是以该地址作为父路径

@RequestMapping("/building")
public class BuildingController {
}

用于方法上: 提供进一步的细分映射信息

@RequestMapping("/building")
public class BuildingController {

	@RequestMapping(value = "/list/{communityId}")
	public BaseDTO list(@PathVariable String communityId ) {
	    // TODO
	}
}

二、属性
1、value
1)说明
指定请求的实际地址,指定的地址可以是URI Template 模式;

2)示例
a.普通的具体值

@RequestMapping(value = "/list")
public BaseDTO list(@PathVariable String communityId ) {
    // TODO
}
//多个请求映射到一个方法
@RequestMapping(value = {  
        "",  
        "/page",  
        "page*"
    })  
public BaseDTO list(@PathVariable String communityId ) {
    // TODO
}

b.含有某变量的一类值(URI Template Patterns with Path Variables)

@RequestMapping(value = "/list/{communityId}")
public BaseDTO list(@PathVariable String communityId ) {
    // TODO
}
c.含正则表达式的一类值( URI Template Patterns with Regular Expressions)
@RequestMapping(value = "/list/{communityId:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")  
public BaseDTO list(@PathVariable String communityId ) {
    // TODO
}

2、method
1)说明
指定请求的method类型, GET、POST、PUT、DELETE等;

2)示例

@RequestMapping(value = "/list" , method = RequestMethod.POST)
public BaseDTO list(@PathVariable String communityId) {
    // TODO
}

3、consumes
1)说明
指定处理请求的 提交内容类型 (Content-Type),例如 application/json, text/html

2)示例

//方法仅处理request Content-Type为“application/json”类型的请求
@RequestMapping(value = "/list" , method = RequestMethod.POST,consumes="application/json")
public void list(@PathVariable String communityId) {
   // TODO
}

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

2)示例

@RequestMapping(value = "/list" , method = RequestMethod.POST,produces="application/json")
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

//@responseBody就是返回值是json数据,使用@responseBody,就可以省略produces属性
@RequestMapping(value = "/list" , method = RequestMethod.POST)
@ResponseBody
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

//返回值是json数据,字符编码为utf-8
@RequestMapping(value = "/list" , method = RequestMethod.POST,produces="application/json;charset=utf-8")
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

5、params
1)说明
指定request中必须包含某些参数值时,才让该方法处理。

2)示例

//设定必须包含username 和age两个参数,且age参数不为10 (可以有多个参数)
@RequestMapping(value = "/list" , method = RequestMethod.POST,params = { "username","age!=10" })
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

6、headers
1)说明
指定request中必须包含某些指定的header值,才能让该方法处理请求。

2)示例

//仅处理request的header中包含了指定“Refer”请求头和对应值为“http://www.ifeng.com/”的请求;
@RequestMapping(value = "/list" , method = RequestMethod.POST,headers="Referer=http://www.ifeng.com/")
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

三、@RequestMapping 快捷方式
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@RequestMapping接口动态参数

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值