Java开发RESTful(六)SpringMVC开发下

【原创文章,转载请注明原文章地址,谢谢!】

请求处理

当后台完成REST服务开发之后,前台需要通过各种方式去请求后台完成的REST服务。那么不同的请求方式,应该怎么处理,是本节要介绍的重点。

客户端访问REST服务,如果客户端是移动端,即iOS或Android,这个不是我们要考虑的目标,我们要考虑的主要是两种客户端请求,Ajax和普通表单请求。而针对这两种请求,重点要考察的是怎么去设置请求类型。

对于Ajax来说,相对会比较简单一些,直接看代码:

<script type="text/javascript">
$(function(){
$("#click").click(function(){
    $.ajax({
        dataType:"json",
        type:"DELETE",
        url:"/emps/2",
        success:function(){
            alert("删除成功!");
        }
    });
});
})
</script>

因为Ajax请求可以直接使用type来指定请求类型,所以很方便。

对于浏览器请求或者普通表单请求,相对就比较困难了。我们知道,直接在浏览器地址栏输入请求,就只能是GET的请求,所以这种方式可以直接忽略,我们重点考虑怎么通过表单去设置不同的请求类型。在HTML中,表单的method属性只能设置GET和POST两种,无法设置其他请求类型。为了达到这个目标,SpringMVC为我们提供了一个很简单的解决方案:

  • 在表单中添加一个hidden域,默认名称为_method,value设置为我们想要的请求方式:
<body>
<a href="javascript:;" id="click">delete emp</a>
<form action="/emps/4" method="POST">
    <input type="hidden" name="_method" value="DELETE" />
    <input type="submit" value="提交" />
</form>
</body>
  • 在应用中,添加一个过滤器(在web.xml中配置)
<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>
  • 完成配置。这时候,只需要正常提交表单,则该请求则会被SpringMVC识别为DELETE类型的请求。

  • 原理:其实原理很简单,重点就在这个HiddenHttpMethodFilter过滤器中,我们把这个过滤器的核心代码简单贴一些:

public class HiddenHttpMethodFilter extends OncePerRequestFilter {

/** Default method parameter: {@code _method} */
public static final String DEFAULT_METHOD_PARAM = "_method";

private String methodParam = DEFAULT_METHOD_PARAM;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {

    String paramValue = request.getParameter(this.methodParam);
    if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
        String method = paramValue.toUpperCase(Locale.ENGLISH);
        HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
        filterChain.doFilter(wrapper, response);
    }
    else {
        filterChain.doFilter(request, response);
    }
}
   //...其余代码
}

可以很清楚的看到,首先请求必须是POST,然后根据_method参数得到对应的_method值(这个地方我们也可以看到,可以通过methodParam参数来重新修改_method这个默认值)。接着把当前请求包装到一个HttpMethodRequestWrapper对象中,然后使用这个Wrapper对象替换原来的Request对象继续传递,这其实是一个很简单的装饰模式,我们来看看这个HttpMethodRequestWrapper对象的重要代码:

private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {

    private final String method;

    public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
        super(request);
        this.method = method;
    }

    @Override
    public String getMethod() {
        return this.method;
    }
}

代码非常简单,就是覆盖了HttpServletRequest的getMethod方法,返回的就是我们从_method获取的值。

小结

至此,SpringMVC关于REST的基本开发介绍完毕,下面的章节会介绍使用Jersey开发REST应用。


猜你喜欢

转载自blog.csdn.net/wolfcode_cn/article/details/80810145
今日推荐