什么是Restful
Restful架构,就是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用。
Restful(即Representational State Transfer的缩写)其实是一个开发理念,是对http的很好地诠释。
1、对URL进行规范,写Restful格式的URL
非Rest的URL:http://.../queryItems.action?id=001&type=T01
Rest的URL风格:http://.../items/001
特点:URL简洁,将参数通过URL传到服务端
2、http的方法规范
不管是删除,添加,更新。使用URL是一致的,如果是进行删除,需要设置http的方法为delete,同理添加。。
后台controller方法:判断http方法,如果是delete执行删除,如果是post执行添加
3、对http的contentType规范
请求时指定contentType,要json数据,设置成json格式的type。
controller方法Restful实现
//itemsView/{id}里面的{id}表示将这个位置的参数传到@PathVariable指定名称中
@RequestMapping("/itemsView/{id}")
public @ResponseBody ItemCustom itemsView(@PathVariable("id")Intege id)throws Exception{
//调用service查询商品信息
ItemCustom itemCustom = itemsSerVice.findItemsById(id);
return itemCustom;
}
备注:在前端控制器配置 <url-pattern>标签中设置为 /
Restful对静态资源的解析
需要在springmvc手动配置静态文件解析
<!--静态资源解析
包括:js、css、img..
-->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>