Rest介紹
/blog/1 HTTP GET => 得到id = 1的blog
/blog/1 HTTP DELETE => 刪除 id = 1的blog
/blog/1 HTTP PUT => 更新id = 1的blog
/blog HTTP POST => 新增BLOG
以下詳細解一下spring rest使用.
首先,我們帶著如下兩個問題查看本文。
1.如何在java構造沒有擴展名的RESTful url,如 /forms/1,而不是 /forms/1.do
2.瀏覽器的form標簽不支持提交delete,put請求,如何曲線解決
springmvc rest 實現
springmvc的resturl是通過@RequestMapping 及@PathVariable annotation提供的,
通過如@RequestMapping(value="/blog /{id}",method=RequestMethod.DELETE)即可處理/blog/1 的delete請求.
1
@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
2
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response)
{
3
blogManager.removeById(id);
4
return new ModelAndView(LIST_ACTION);
5
}
@RequestMapping @PathVariable如果URL中帶參數,則配合使用,如
1
@RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
2
public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response)
{
3
}
1.springmvc web.xml配置
1
<!-- 該servlet為tomcat,jetty等容器提供,將靜態資源映射從/改為/static/目錄,如原來訪問 http://localhost/foo.css ,現在http://localhost/static/foo.css -->
2
<servlet-mapping>
3
<servlet-name>default</servlet-name>
4
<url-pattern>/static/*</url-pattern>
5
</servlet-mapping>
6
<servlet>
7
<servlet-name>springmvc</servlet-name>
8
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9
<load-on-startup>1</load-on-startup>
10
</servlet>
11
12
<!-- URL重寫filter,用于將訪問靜態資源http://localhost/foo.css 轉為http://localhost/static/foo.css -->
13
<filter>
14
<filter-name>UrlRewriteFilter</filter-name>
15
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
16
<init-param>
17
<param-name>confReloadCheckInterval</param-name>
18
<param-value>60</param-value>
19
</init-param>
20
<init-param>
21
<param-name>logLevel</param-name>
22
<param-value>DEBUG</param-value>
23
</init-param>
24
</filter>
25
<filter-mapping>
26
<filter-name>UrlRewriteFilter</filter-name>
27
<url-pattern>/*</url-pattern>
28
</filter-mapping>
29
30
<!-- 覆蓋default servlet的/, springmvc servlet將處理原來處理靜態資源的映射 -->
31
<servlet-mapping>
32
<servlet-name>springmvc</servlet-name>
33
<url-pattern>/</url-pattern>
34
</servlet-mapping>
35
36
<!-- 瀏覽器不支持put,delete等method,由該filter將/blog?_method=delete轉換為標準的http delete方法 -->
37
<filter>
38
<filter-name>HiddenHttpMethodFilter</filter-name>
39
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
40
</filter>
41
42
<filter-mapping>
43
<filter-name>HiddenHttpMethodFilter</filter-name>
44
<servlet-name>springmvc</servlet-name>
45
</filter-mapping>
2.webapp/WEB-INF/springmvc-servlet.xml配置,使用如下兩個class激活@RequestMapping annotation
1
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
2
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
3
3.Controller編寫
1
/** *//**
2
* @RequestMapping("/userinfo") 具有層次關系,方法級的將在類一級@RequestMapping之一,
3
* 如下面示例, 訪問方法級別的@RequestMapping("/new"),則URL為 /userinfo/new
4
*/
5
@Controller
6
@RequestMapping("/userinfo")
7
public class UserInfoController extends BaseSpringController
{
8
//默認多列排序,example: username desc,createTime asc
9
protected static final String DEFAULT_SORT_COLUMNS = null;
10
11
private UserInfoManager userInfoManager;
12
13
private final String LIST_ACTION = "redirect:/userinfo";
14
15
/** *//**
16
* 通過spring自動注入
17
**/
18
public void setUserInfoManager(UserInfoManager manager)
{
19
this.userInfoManager = manager;
20
}
21
22
/** *//** 列表 */
23
@RequestMapping
24
public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo)
{
25
PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
26
//pageRequest.getFilters(); //add custom filters
27
28
Page page = this.userInfoManager.findByPageRequest(pageRequest);
29
savePage(page,pageRequest,request);
30
return new ModelAndView("/userinfo/list","userInfo",userInfo);
31
}
32
33
/** *//** 進入新增 */
34
@RequestMapping(value="/new")
35
public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception
{
36
return new ModelAndView("/userinfo/new","userInfo",userInfo);
37
}
38
39
/** *//** 顯示 */
40
@RequestMapping(value="/{id}")
41
public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception
{
42
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
43
return new ModelAndView("/userinfo/show","userInfo",userInfo);
44
}
45
46
/** *//** 編輯 */
47
@RequestMapping(value="/{id}/edit")
48
public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception
{
49
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
50
return new ModelAndView("/userinfo/edit","userInfo",userInfo);
51
}
52
53
/** *//** 保存新增 */
54
@RequestMapping(method=RequestMethod.POST)
55
public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception
{
56
userInfoManager.save(userInfo);
57
return new ModelAndView(LIST_ACTION);
58
}
59
60
/** *//** 保存更新 */
61
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
62
public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception
{
63
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
64
bind(request,userInfo);
65
userInfoManager.update(userInfo);
66
return new ModelAndView(LIST_ACTION);
67
}
68
69
/** *//** 刪除 */
70
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
71
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response)
{
72
userInfoManager.removeById(id);
73
return new ModelAndView(LIST_ACTION);
74
}
75
76
/** *//** 批量刪除 */
77
@RequestMapping(method=RequestMethod.DELETE)
78
public ModelAndView batchDelete(HttpServletRequest request,HttpServletResponse response)
{
79
String[] items = request.getParameterValues("items");
80
for(int i = 0; i < items.length; i++)
{
81
java.lang.Long id = new java.lang.Long(items[i]);
82
userInfoManager.removeById(id);
83
}
84
return new ModelAndView(LIST_ACTION);
85
}
86
87
}
88
1
/userinfo => index()
2
/userinfo/new => _new()
3
/userinfo/{id} => show()
4
/userinfo/{id}/edit => edit()
5
/userinfo POST => create()
6
/userinfo/{id} PUT => update()
7
/userinfo/{id} DELETE => delete()
8
/userinfo DELETE => batchDelete()
9
注(不使用 /userinfo/add => add() 方法是由于add這個方法會被maxthon瀏覽器當做廣告鏈接過濾掉,因為包含ad字符)
4.jsp 編寫
1
<form:form action="${ctx}/userinfo${userInfo.userId}" method="put">
2
</form:form>
生成的html內容如下, 生成一個hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服務端將post請求改為put請求
1
<form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
2
<input type="hidden" name="_method" value="put"/>
3
</form>
另外一種方法是你可以使用ajax發送put,delete請求.
5.靜態資源的URL重寫
如上我們描述,現因為將default servlet映射至/static/的子目錄,現我們訪問靜態資源將會帶一個/static/前綴.
如 /foo.gif, 現在訪問該文件將是 /static/foo.gif.
那如何避免這個前綴呢,那就是應用URL rewrite,現我們使用 http://tuckey.org/urlrewrite/, 重寫規則如下
1
<urlrewrite>
2
<!-- 訪問jsp及jspx將不rewrite url,其它.js,.css,.gif等將重寫,如 /foo.gif => /static/foo.gif -->
3
<rule>
4
<condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
5
<condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
6
<from>^(/.*\..*)$</from>
7
<to>/static$1</to>
8
</rule>
9
</urlrewrite>
posted on 2012-03-08 21:27
soken 閱讀(161270)
評論(0) 編輯 收藏 所屬分類:
Java