spring 3.0 应用springmvc 构造RESTful URL 详细讲解
来源:互联网 发布:学西班牙语的软件 编辑:程序博客网 时间:2024/06/06 21:38
(一)
简单例子如下,比如如下URL
- /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
/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. 由于我们要构造没有扩展名的url本来是处理静态资源的容器映射的,现在被我们的spring占用了,冲突怎么解决?
3. 浏览器的form标签不支持提交delete,put请求,如何曲线解决?
springmvc rest 实现
springmvc的resturl是通过@RequestMapping 及@PathVariable annotation提供的,通过如@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)即可处理/blog/1 的delete请求.
- @RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
- public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
- blogManager.removeById(id);
- return new ModelAndView(LIST_ACTION);
- }
@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {blogManager.removeById(id);return new ModelAndView(LIST_ACTION);}
@RequestMapping @PathVariable如果URL中带参数,则配合使用,如
- @RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
- public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {
- }
@RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {}
spring rest配置指南
1. springmvc web.xml配置
- <!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>/static/*</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->
- <filter>
- <filter-name>UrlRewriteFilter</filter-name>
- <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
- <init-param>
- <param-name>confReloadCheckInterval</param-name>
- <param-value>60</param-value>
- </init-param>
- <init-param>
- <param-name>logLevel</param-name>
- <param-value>DEBUG</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>UrlRewriteFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
- <filter>
- <filter-name>HiddenHttpMethodFilter</filter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilter</filter-name>
- <servlet-name>springmvc</servlet-name>
- </filter-mapping>
<!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css --><servlet-mapping><servlet-name>default</servlet-name><url-pattern>/static/*</url-pattern></servlet-mapping><servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup></servlet><!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css --><filter><filter-name>UrlRewriteFilter</filter-name><filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class><init-param> <param-name>confReloadCheckInterval</param-name> <param-value>60</param-value> </init-param><init-param> <param-name>logLevel</param-name> <param-value>DEBUG</param-value> </init-param> </filter><filter-mapping><filter-name>UrlRewriteFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 --><servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern></servlet-mapping><!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><servlet-name>springmvc</servlet-name></filter-mapping>
2. webapp/WEB-INF/springmvc-servlet.xml配置,使用如下两个class激活@RequestMapping annotation
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
完整配置
- <beans default-autowire="byName" >
- <!-- 自动搜索@Controller标注的类 -->
- <context:component-scan base-package="com.**.controller"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
- <!-- Default ViewResolver -->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/pages"/>
- <property name="suffix" value=".jsp"></property>
- </bean>
- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>
- <!-- Mapping exception to the handler view -->
- <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
- <!-- to /commons/error.jsp -->
- <property name="defaultErrorView" value="/commons/error"/>
- <property name="exceptionMappings">
- <props>
- </props>
- </property>
- </bean>
- </beans>
<beans default-autowire="byName" ><!-- 自动搜索@Controller标注的类 --><context:component-scan base-package="com.**.controller"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- Default ViewResolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/pages"/> <property name="suffix" value=".jsp"></property> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> <!-- Mapping exception to the handler view --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- to /commons/error.jsp --> <property name="defaultErrorView" value="/commons/error"/> <property name="exceptionMappings"> <props> </props> </property> </bean> </beans>
3. Controller编写
- /**
- * @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一,
- * 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new
- */
- @Controller
- @RequestMapping("/userinfo")
- public class UserInfoController extends BaseSpringController{
- //默认多列排序,example: username desc,createTime asc
- protected static final String DEFAULT_SORT_COLUMNS = null;
- private UserInfoManager userInfoManager;
- private final String LIST_ACTION = "redirect:/userinfo";
- /**
- * 通过spring自动注入
- **/
- public void setUserInfoManager(UserInfoManager manager) {
- this.userInfoManager = manager;
- }
- /** 列表 */
- @RequestMapping
- public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {
- PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
- //pageRequest.getFilters(); //add custom filters
- Page page = this.userInfoManager.findByPageRequest(pageRequest);
- savePage(page,pageRequest,request);
- return new ModelAndView("/userinfo/list","userInfo",userInfo);
- }
- /** 进入新增 */
- @RequestMapping(value="/new")
- public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
- return new ModelAndView("/userinfo/new","userInfo",userInfo);
- }
- /** 显示 */
- @RequestMapping(value="/{id}")
- public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
- UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
- return new ModelAndView("/userinfo/show","userInfo",userInfo);
- }
- /** 编辑 */
- @RequestMapping(value="/{id}/edit")
- public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
- UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
- return new ModelAndView("/userinfo/edit","userInfo",userInfo);
- }
- /** 保存新增 */
- @RequestMapping(method=RequestMethod.POST)
- public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
- userInfoManager.save(userInfo);
- return new ModelAndView(LIST_ACTION);
- }
- /** 保存更新 */
- @RequestMapping(value="/{id}",method=RequestMethod.PUT)
- public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
- UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
- bind(request,userInfo);
- userInfoManager.update(userInfo);
- return new ModelAndView(LIST_ACTION);
- }
- /** 删除 */
- @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
- public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
- userInfoManager.removeById(id);
- return new ModelAndView(LIST_ACTION);
- }
- /** 批量删除 */
- @RequestMapping(method=RequestMethod.DELETE)
- public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {
- for(int i = 0; i < items.length; i++) {
- userInfoManager.removeById(items[i]);
- }
- return new ModelAndView(LIST_ACTION);
- }
- }
/** * @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一, * 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new */@Controller@RequestMapping("/userinfo")public class UserInfoController extends BaseSpringController{//默认多列排序,example: username desc,createTime ascprotected static final String DEFAULT_SORT_COLUMNS = null; private UserInfoManager userInfoManager;private final String LIST_ACTION = "redirect:/userinfo";/** * 通过spring自动注入 **/public void setUserInfoManager(UserInfoManager manager) {this.userInfoManager = manager;}/** 列表 */@RequestMappingpublic ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);//pageRequest.getFilters(); //add custom filtersPage page = this.userInfoManager.findByPageRequest(pageRequest);savePage(page,pageRequest,request);return new ModelAndView("/userinfo/list","userInfo",userInfo);}/** 进入新增 */@RequestMapping(value="/new")public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {return new ModelAndView("/userinfo/new","userInfo",userInfo);}/** 显示 */@RequestMapping(value="/{id}")public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {UserInfo userInfo = (UserInfo)userInfoManager.getById(id);return new ModelAndView("/userinfo/show","userInfo",userInfo);}/** 编辑 */@RequestMapping(value="/{id}/edit")public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {UserInfo userInfo = (UserInfo)userInfoManager.getById(id);return new ModelAndView("/userinfo/edit","userInfo",userInfo);}/** 保存新增 */@RequestMapping(method=RequestMethod.POST)public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {userInfoManager.save(userInfo);return new ModelAndView(LIST_ACTION);}/** 保存更新 */@RequestMapping(value="/{id}",method=RequestMethod.PUT)public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {UserInfo userInfo = (UserInfo)userInfoManager.getById(id);bind(request,userInfo);userInfoManager.update(userInfo);return new ModelAndView(LIST_ACTION);}/** 删除 */@RequestMapping(value="/{id}",method=RequestMethod.DELETE)public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {userInfoManager.removeById(id);return new ModelAndView(LIST_ACTION);}/** 批量删除 */@RequestMapping(method=RequestMethod.DELETE)public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {for(int i = 0; i < items.length; i++) {userInfoManager.removeById(items[i]);}return new ModelAndView(LIST_ACTION);}}
上面是rapid-framework新版本生成器生成的代码,以后也将应用此规则,rest url中增删改查等基本方法与Controller的方法映射规则
- /userinfo => index()
- /userinfo/new => _new()
- /userinfo/{id} => show()
- /userinfo/{id}/edit => edit()
- /userinfo POST => create()
- /userinfo/{id} PUT => update()
- /userinfo/{id} DELETE => delete()
- /userinfo DELETE => batchDelete()
/userinfo => index()/userinfo/new=> _new()/userinfo/{id}=> show()/userinfo/{id}/edit => edit()/userinfo POST=> create()/userinfo/{id} PUT=> update()/userinfo/{id} DELETE=> delete()/userinfo DELETE=> batchDelete()
注(不使用 /userinfo/add => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)
4. jsp 编写
- <form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">
- </form:form>
<form:form action="${ctx}/userinfo/${userInfo.userId}" method="put"></form:form>
生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求
- <form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
- <input type="hidden" name="_method" value="put"/>
- </form>
<form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post"><input type="hidden" name="_method" value="put"/></form>
另外一种方法是你可以使用ajax发送put,delete请求.
5. 静态资源的URL重写
如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀.
如 /foo.gif, 现在访问该文件将是 /static/foo.gif.
那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下
- <urlrewrite>
- <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->
- <rule>
- <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
- <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
- <from>^(/.*\..*)$</from>
- <to>/static$1</to>
- </rule>
- </urlrewrite>
<urlrewrite> <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif --> <rule> <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition> <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition> <from>^(/.*\..*)$</from> <to>/static$1</to> </rule></urlrewrite>
另笔者专门写了一个 RestUrlRewriteFilter来做同样的事件,以后会随着rapid-framework一起发布. 比这个更加轻量级.
并且该代码已经贡献给spring,不知会不会在下一版本发布
(二)
RESTful服务中很重要的一个特性即是同一资源,多种表述.也即如下面描述的三种方式:
- GET /user/123 HTTP/1.1
- Accept: application/xml //将返回xml格式数据
- GET /user/123 HTTP/1.1
- Accept: application/json //将返回json格式数据
GET /user/123 HTTP/1.1Accept: application/xml //将返回xml格式数据GET /user/123 HTTP/1.1Accept: application/json //将返回json格式数据
- /user/123.xml 将返回xml格式数据
- /user/123.json 将返回json格式数据
- /user/123.html 将返回html格式数据
/user/123.xml 将返回xml格式数据/user/123.json 将返回json格式数据/user/123.html 将返回html格式数据
- /user/123?format=xml //将返回xml数据
- /user/123?format=json //将返回json数据
/user/123?format=xml //将返回xml数据/user/123?format=json //将返回json数据
- chrome:
- Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
- firefox:
- Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- IE8:
- Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
chrome:Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5firefox:Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8IE8:Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
二.spring配置
- <!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml -->
- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <!-- 设置为true以忽略对Accept Header的支持-->
- <property name="ignoreAcceptHeader" value="true"/>
- <!-- 在没有扩展名时即: "/user/1" 时的默认展现形式 -->
- <property name="defaultContentType" value="text/html"/>
- <!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
- <property name="mediaTypes">
- <map>
- <entry key="json" value="application/json" />
- <entry key="xml" value="application/xml" />
- </map>
- </property>
- <!-- 用于开启 /userinfo/123?format=json 的支持 -->
- <property name="favorParameter" value="false"/>
- <property name="viewResolvers">
- <list>
- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/pages"/>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </list>
- </property>
- <property name="defaultViews">
- <list>
- <!-- for application/json -->
- <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
- <!-- for application/xml -->
- <!--
- <bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
- <property name="marshaller">
- <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
- </property>
- </bean>
- -->
- </list>
- </property>
- </bean>
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- Spring--spring3.0应用springmvc构造RESTful URL详细讲解
- springmvc 构造RESTful URL 详细讲解
- Spring mvc 构造RESTful URL 详细讲解(spring 3.0 应用)
- Springmvc构造RESTful详细讲解
- Springmvc构造RESTful详细讲解
- Springmvc构造RESTful详细讲解
- Springmvc构造RESTful详细讲解
- Springmvc构造RESTful详细讲解
- Springmvc构造RESTful详细讲解
- spring aop
- AliasedResultTransformer hibernate的结果集转换
- 【Android】菜单功能的实现:弹出Popup菜单 以及小结
- 基于Solr的LBS(地理位置搜索)实现原理
- git使用详解
- spring 3.0 应用springmvc 构造RESTful URL 详细讲解
- Android应用开发之即时通信
- 一个数转化成1的最小步数
- Jenkins自动打tag报错
- 动态网站制作指南网址
- Unity3D开发之Unity3D性能优化总结
- hello world!
- Liunx下查看服务器性能
- 关于fork()返回值