版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。
https://blog.csdn.net/weixin_43453386/article/details/83419060
RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。
@RequestMapping("/building")
public class BuildingController {
}
@RequestMapping("/building")
public class BuildingController {
@RequestMapping(value = "/list/{communityId}")
public BaseDTO list(@PathVariable String communityId ) {
// TODO
}
}
指定请求的实际地址,指定的地址可以是URI Template 模式;
@RequestMapping(value = "/list")
public BaseDTO list(@PathVariable String communityId ) {
// TODO
}
//多个请求映射到一个方法
@RequestMapping(value = {
"",
"/page",
"page*"
})
public BaseDTO list(@PathVariable String communityId ) {
// TODO
}
@RequestMapping(value = "/list/{communityId}")
public BaseDTO list(@PathVariable String communityId ) {
// TODO
}
@RequestMapping(value = "/list/{communityId:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
public BaseDTO list(@PathVariable String communityId ) {
// TODO
}
指定请求的method类型, GET、POST、PUT、DELETE等;
@RequestMapping(value = "/list" , method = RequestMethod.POST)
public BaseDTO list(@PathVariable String communityId) {
// TODO
}
指定处理请求的 提交内容类型 (Content-Type),例如 application/json, text/html
//方法仅处理request Content-Type为“application/json”类型的请求
@RequestMapping(value = "/list" , method = RequestMethod.POST,consumes="application/json")
public void list(@PathVariable String communityId) {
// TODO
}
指定 返回的内容类型 ,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
@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;
}
指定request中必须包含某些参数值时,才让该方法处理。
//设定必须包含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;
}
指定request中必须包含某些指定的header值,才能让该方法处理请求。
//仅处理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;
}