springboot RESTful及参数注解使用
RESTful
Spring的复杂性不是来自于它处理的对象,而是来自于自身,不断演进发展的Spring会带来时间维度上复杂性,比如SpringMVC以前版本的@RequestMapping,到了新版本被下面新注释替代,相当于增加的选项:
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
说明
1、@GetMapping
@RequestMapping(method = RequestMethod.GET)的简写
作用:对应查询,表明是一个查询URL映射
2、@PostMapping
@RequestMapping(method = RequestMethod.POST)的简写
作用:对应增加,表明是一个增加URL映射
3、@PutMapping
@RequestMapping(method = RequestMethod.PUT)的简写
作用:对应更新,表明是一个更新URL映射
4、@DeleteMapping
@RequestMapping(method = RequestMethod.DELETE)的简写
作用:对应删除,表明是一个删除URL映射
5、@PatchMapping
Patch方式是对put方式的一种补充;
put方式是可以更新.但是更新的是整体.patch是对局部更新;
参数注解的使用
- @PathVariable
- @RequestParam
- @RequestBody
- @ModelAttribute
说明
1. @PathVariable
获取路径参数。即url/{id}这种形式
@PathVariable绑定URI模板变量值
@PathVariable是用来获得请求url中的动态参数的
@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。//配置url和方法的一个关系@RequestMapping(“item/{itemId}”)
2.@RequestParam
获取查询参数。即url?name=这种形式
@RequestParam注解主要有哪些参数:
value
:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required
:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue
:默认值,表示如果请求中没有同名参数时的默认值,例如:
- public List getItemTreeNode(@RequestParam(value=“id”,defaultValue=“0”)long parentId)
3.@RequestBody
@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是application/xml等。一般情况下来说常用其来处理application/json类型。
通过@requestBody可以将请求体中的JSON字符串绑定到相应的bean上,当然,也可以将其分别绑定到对应的字符串上。
4.@ModelAttribute
在使用RESTful风格时,使用get请求,又想使用对象接收参数,就可以使用这个注解
不光适用于get请求,同样也适用于put和delete请求
springboot Restful使用记录
创建项目
通过spring官网创建项目
https://start.spring.io/

Rest组件使用
使用@RestController标记类为提供Restful服务的Contoller
@GetMapping为资源定位一部分,也就是url,对应http://localhost:8080/test
- @RestController
- public class MyRestContoller1 {
- @GetMapping("/test")
- public Map<String, String> getData() {
- Map<String, String> data = new HashMap<String, String>();
- data.put("id", "111");
- data.put("name", "zhangsan");
- return data;
- }
- }
测试(这里使用浏览器测试,后续使用Postman工具)

@GetMapping关键字对应GET请求,也就是查询,请求还可以有参数,对应@PathVariable与@RequestParam注解
- @GetMapping("/test/{id}")
- public Map<String, String> getData2(@PathVariable String id, @RequestParam(required = false) String name) {
- Map<String, String> data = new HashMap<String, String>();
- data.put("id", id);
- data.put("name", name);
- return data;
- }
测试,返回值为入参传入参数

Post类型,新增操作
新增使用@PostMapping描述URL
新增一般都会带有大量数据,一般都是使用@RequestBody注解封装参数
- @PostMapping("/test2/add")
- public Map<String, String> addData(@RequestBody Map<String, String> data) {
- return data;
- }
测试

注意两点,不正确都会报错
- 请求类型必须是POST
- Content-type必须要设置为application/json,因为入参形式为JSON格式

更新与删除操作
使用上与Post一致,只是不同类型需要使用对应的主机
PUT
:@PutMapping
DELETE
:@DeleteMapping
- @PutMapping("/test2/update")
- public Map<String, String> updateData(@RequestBody Map<String, String> data) {
- return data;
- }
-
- @DeleteMapping("/test2/delete")
- public Map<String, String> deleteData(@RequestBody Map<String, String> data) {
- return data;
- }
RequestMapping使用
RequestMapping是一个通用注解,包含上述所有操作
- @RestController
- @RequestMapping(value = "/parent")
- public class RequestRestContoller {
- @RequestMapping(value = "/get", method = RequestMethod.GET)
- public Map<String, String> get() {
- Map<String, String> data = new HashMap<String, String>();
- data.put("id", "111");
- data.put("name", "zhangsan");
- return data;
- }
-
- @RequestMapping(value = "/add", method = RequestMethod.POST)
- public Map<String, String> add() {
- Map<String, String> data = new HashMap<String, String>();
- data.put("id", "111");
- data.put("name", "zhangsan");
- return data;
- }
-
- @RequestMapping(value = "/update", method = RequestMethod.PUT)
- public Map<String, String> update() {
- Map<String, String> data = new HashMap<String, String>();
- data.put("id", "111");
- data.put("name", "zhangsan");
- return data;
- }
-
- @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
- public Map<String, String> delete() {
- Map<String, String> data = new HashMap<String, String>();
- data.put("id", "111");
- data.put("name", "zhangsan");
- return data;
- }
- }
上述还有贴在class上面的注解:@RequestMapping(value = "/parent"),如果是class上面的注解,那么方法上面的url需要加上class上面的注解
如:http://localhost:8080/parent/get或http://localhost:8080/parent/add
其中可以属于请求参数和响应数据类型
- @RequestMapping(value = "/parent", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
其中consumes 约束入参类型,produces 约束响应数据类型
测试Content-Type:text/plain报错,由于设置了JSON格式

支持哪些格式参考Media定义
- org.springframework.http.MediaType
XML格式数据支持
这里扩展一下,返回XML格式数据
引入XML依赖包
- <dependency>
- <groupId>com.fasterxml.jackson.dataformat</groupId>
- <artifactId>jackson-dataformat-xml</artifactId>
- </dependency>
测试类
- @RestController
- public class DataRestContoller {
- @RequestMapping(value = "/addJsonResponseXml", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
- public Map<String, String> add(@RequestBody Map<String, String> data) {
- return data;
- }
- }
测试

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。