创建项目
通过spring官网创建项目
https://start.spring.io/
- 项目名称取为studyRest
- 项目依赖WEB
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;
}
}
测试