1. HTTP知识
名词 | 解释 |
---|---|
GET | 请求获取Request-URI所标识的资源 |
POST | 在Request-URI所标识的资源后附加新的数据 |
HEAD | 请求获取由Request-URI所标识的资源的响应消息报头 |
PUT | 请求服务器存储一个资源,并用Request-URI作为其标识 |
DELETE | 请求服务器删除Request-URI所标识的资源 |
TRACE | 请求服务器回送收到的请求信息,主要用于测试或诊断 |
CONNECT | 保留将来使用 |
OPTIONS | 请求查询服务器的性能,或者查询与资源相关的选项和需求 |
2. restful ?
最近学习了一下restful风格,简而言之,就是一种接口访问方式+命名的规范。
具体一点, GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源。
牛人总结restful :
(1)每一个URI代表一种资源;
(2)客户端和服务器之间,传递这种资源的某种表现层;
(3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现”表现层状态转化”。
原文
3.代码demo
- 查询单个对象(GET + 路径传参)
@RequestMapping(value = "/city/{id}",method = RequestMethod.GET)
public BaseResp<City> getCity(@PathVariable Long id){
return new BaseResp(ResultStatus.SUCCESS,restfulService.getCity(id));
}
- 查询列表(GET)
@RequestMapping(value = "/city",method = RequestMethod.GET)
public BaseResp<List<City>> getCity(){
return new BaseResp(ResultStatus.SUCCESS,restfulService.listCity());
}
- 查询分页(GET)
@RequestMapping(value = "/city/{pages}/{size}",method = RequestMethod.GET)
public BaseResp<Page<City>> getCity(@PathVariable Integer pages,
@PathVariable Integer size){
return new BaseResp(ResultStatus.SUCCESS,restfulService.listCity(pages, size));
}
- 保存对象(POST)
@RequestMapping(value = "/city",method = RequestMethod.POST)
public BaseResp<City> saveCity(@RequestBody City city){
return new BaseResp(ResultStatus.SUCCESS,restfulService.saveCity(city));
}
- 修改对象(PUT)
@RequestMapping(value = "/city/{id}",method = RequestMethod.PUT)
public BaseResp<Integer> saveCity(@PathVariable Long id,
@RequestBody City city){
return new BaseResp(ResultStatus.SUCCESS,restfulService.updateCity(id,city));
}
- 删除(DELETE)
@RequestMapping(value = "/city/{id}",method = RequestMethod.DELETE)
public BaseResp<String> deleteCity(@PathVariable Long id){
restfulService.deleteCity(id);
return new BaseResp(ResultStatus.SUCCESS);
}
- 上传文件demo
@RequestMapping(value = "/upload/{typeId}")
public BaseResp<String> uploadFile(@PathVariable Integer typeId,
@RequestParam("file") MultipartFile file){
if (null != file){
System.out.println(typeId);
System.out.println(file.getOriginalFilename());
}
return new BaseResp(ResultStatus.SUCCESS);
}