Spring|Spring 处理请求和响应相关的注解
以 String 接收数据
@RequestMapping("/index")
public String indexMapping(@RequestBody String jsonStr) {
return jsonStr;
}
以对象实体接收数据
// {"name":"hanmeimei","age":12}
@RequestMapping("/index")
public String indexMapping(@RequestBody User user) {
return user.toString();
}
以复杂的对象实体接收数据
public class Team {
private Integer id;
private String name;
private List honors;
private List
}
// {
//"id": 1,
//"name": "good",
//"honors": ["very good", "very fast"],
//"members": [{"name":"hanmeimei","age":12},
//{"name":"lilei","age":13}],
// }
@RequestMapping("/index")
public String indexMapping(@RequestBody Team team) {
return team.toString();
}
@ResponseBody
将对象实体转换为JSON、XML或自定义mediaType的内容,并在 HTTP response body 中返回
@RequestMapping
将请求映射到控制器上,可以在控制器类和/或方法上使用。
处理单个请求
@RequestMapping("/home")
public class IndexController {
@RequestMapping("/index")
String indexMapping() {
return "Hello from index mapping.";
}
}
处理多个请求
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = https://www.it610.com/article/{
"/",
"/index",
"/index/*.html",
"/index/**/*.html"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.";
}
}
处理请求类型
默认是 HTTP GET 类型的。
@RequestMapping(value = "https://www.it610.com/home", method = RequestMethod.GET)
String get() {}
@RequestMapping(value = "https://www.it610.com/home", method = RequestMethod.DELETE)
String delete() {}
@RequestMapping(value = "https://www.it610.com/home", method = RequestMethod.POST)
String post() {}
@RequestMapping(value = "https://www.it610.com/home", method = RequestMethod.PUT)
String put() {}
@RequestMapping(value = "https://www.it610.com/home", method = RequestMethod.PATCH)
String patch() {}
处理请求类型快捷方式
@GetMapping(value = "https://www.it610.com/home")
String get() {}
@DeleteMapping(value = "https://www.it610.com/home")
String delete() {}
@PostMapping(value = "https://www.it610.com/home")
String post() {}
@PutMapping(value = "https://www.it610.com/home")
String put() {}
@PatchMapping(value = "https://www.it610.com/home")
String patch() {}
处理生产和消费对象
public class IndexController {
// 生产 application/JSON 响应
@RequestMapping(value = "https://www.it610.com/prod", produces = {
"application/JSON"
})
@ResponseBody
String getProduces() {
return "Produces attribute";
}
// 消费 application/JSON & application/XML 请求
@RequestMapping(value = "https://www.it610.com/cons", consumes = {
"application/JSON",
"application/XML"
})
@ResponseBody
String getConsumes() {
return "Consumes attribute";
}
}
处理消息头
public class IndexController {
// 处理 Content-Type=application/json 的请求
@RequestMapping(value = "https://www.it610.com/head", headers = {
"Content-Type=application/json"
})
String head() {
return "Mapping applied along with headers";
}
}
public class IndexController {
@RequestMapping(value = "https://www.it610.com/head", headers = {
"Content-Type=text/plain",
"Content-Type=application/json"
})
String head() {
return "Mapping applied along with headers";
}
}
处理请求参数
public class IndexController {
@RequestMapping(value = "https://www.it610.com/fetch", params = {
"personId=10"
})
String getParams10(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
@RequestMapping(value = "https://www.it610.com/fetch", params = {
"personId=20"
})
String getParams20(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
}
处理动态 URI
public class IndexController {
@RequestMapping(value = "https://www.it610.com/fetch/{id}")
String getDynamicUriValue(@PathVariable String id) {
return "Dynamic URI parameter fetched";
}
@RequestMapping(value = "https://www.it610.com/fetch/{id:/d+}/{name}")
String getDynamicUriValueRegex(
@PathVariable("id") int id, @PathVariable("name") String name
) {
return "Dynamic URI parameter fetched using regex";
}
}
默认的处理方法
public class IndexController {
@RequestMapping()
String default () {
return "This is a default method for the class";
}
}
【Spring|Spring 处理请求和响应相关的注解】亚马逊测评www.yisuping.com
推荐阅读
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一百二十三夜,请嫁给我
- Activiti(一)SpringBoot2集成Activiti6
- 拍照一年啦,如果你想了解我,那就请先看看这篇文章
- SpringBoot调用公共模块的自定义注解失效的解决
- Java|Java OpenCV图像处理之SIFT角点检测详解
- 解决SpringBoot引用别的模块无法注入的问题
- 事件处理程序
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- 请给时间一点时间