REST 是属于 WEB 自身的一种架构风格,是在HTTP1.1规范下实现的。Representational State Transfer 全称翻译为表现层状态转化。
- Resource:资源。比如 newsfeed;
- Representational:表现形式,比如用JSON,富文本等;
- State Transfer:状态变化。
通过HTTP 动作实现。
CityRestController.java实现 Restful HTTP 服务
public class CityRestController {
@Autowired
private CityService cityService;
@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
public City findOneCity(@PathVariable("id") Long id) {
return cityService.findCityById(id);
}
@RequestMapping(value = "/api/city", method = RequestMethod.GET)
public List<City> findAllCity() {
return cityService.findAllCity();
}
@RequestMapping(value = "/api/city", method = RequestMethod.POST)
public void createCity(@RequestBody City city) {
cityService.saveCity(city);
}
@RequestMapping(value = "/api/city", method = RequestMethod.PUT)
public void modifyCity(@RequestBody City city) {
cityService.updateCity(city);
}
@RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)
public void modifyCity(@PathVariable("id") Long id) {
cityService.deleteCity(id);
}
}
上面代码中的注解解释:
- @RequestMapping 处理请求地址映射
> > method - 指定请求的方法类型:POST/GET/DELETE/PUT 等
> > value - 指定实际的请求地址
> > consumes - 指定处理请求的提交内容类型,例如 Content-Type 头部设置application/json, text/html
> > produces - 指定返回的内容类型
@PathVariable URL 映射时,用于绑定请求参数到方法参数
@RequestBody 这里注解用于读取请求体body的数据,通过HttpMessageConverter 解析绑定到对象中
HTTP知识补充
GET 请求获取Request-URI所标识的资源
POST 在Request-URI所标识的资源后附加新的数据
HEAD 请求获取由Request-URI所标识的资源的响应消息报头
PUT 请求服务器存储一个资源,并用Request-URI作为其标识
DELETE 请求服务器删除Request-URI所标识的资源
TRACE 请求服务器回送收到的请求信息,主要用于测试或诊断
CONNECT 保留将来使用
OPTIONS 请求查询服务器的性能,或者查询与资源相关的选项和需求