java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springmvc Rest风格

springmvc Rest风格介绍及实现代码示例

作者:RexFang

这篇文章主要介绍了springmvc Rest风格介绍及实现代码示例,rest风格简洁,分享了HiddenHttpMethodFilter 的源码,通过Spring4.0实现rest风格源码及简单错误分析,具有一定参考价值,需要的朋友可以了解下。

简介

  REST 即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用,POST, DELETE, PUT, GET 分别对应 CRUD。Spring3.0 开始支持 REST 风格的请求,是通过 org.springframework.web.filter.HiddenHttpMethodFilter 把 POST 请求转化为 PUT 和 DELETE 请求。本次实验采用的是 Spring4.0 。

HiddenHttpMethodFilter 源码

public static final String DEFAULT_METHOD_PARAM = "_method";
@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);
	}
}

  从 HiddenHttpMethodFilter 的源码可以看出,Spring 根据请求中的 _method 参数进行转化,因此如果想发起 REST 风格的 DELETE 或者 PUT 请求,只需要在表单中带上 _method 参数,并且把 _method 的值设置为 DELETE 或者 PUT(大写) 即可。详细例子如下:

在 web.xml 中配置 HiddenHttpMethodFilter
编写 handler 代码
编写页面

<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter ,可以把 POST 请求转化为 PUT 或者 DELETE 请求 -->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
package rex.springmvc.handlers;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping(value="/restTest")
@Controller
public class RestTestHandler {
	private static final Logger logger = Logger.getLogger(RestTestHandler.class);
	private static final String SUCCESS = "success";
	@RequestMapping(value="/restGet/{id}", method=RequestMethod.GET)
	  public String restGet(@RequestParam(value="id", required=false) Integer id){
		logger.debug("restGet:" + id);
		return SUCCESS;
	}
	@RequestMapping(value="/restPut/{id}", method=RequestMethod.PUT)
	  public String restPut(@RequestParam(value="id", required=false) Integer id){
		logger.debug("restPut:" + id);
		return SUCCESS;
	}
	@RequestMapping(value="/restDelete/{id}", method=RequestMethod.DELETE)
	  public String restDelete(@RequestParam(value="id", required=false) Integer id){
		logger.debug("restDelete:" + id);
		return SUCCESS;
	}
	@RequestMapping(value="/restPost", method=RequestMethod.POST)
	  public String restPost(){
		logger.debug("restPost");
		return SUCCESS;
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rest Test</title>
</head>
<body>
  <br>
  <br>
  <a href="restTest/restGet/1" rel="external nofollow" >Test Rest Get</a>

  <br>
  <br>
  <form action="restTest/restPut/1" method="post">
    <input type="hidden" name="_method" value="PUT"> <input
      type="submit" value="submit">
  </form>

  <br>
  <br>
  <form action="restTest/restDelete/1" method="post">
    <input type="hidden" name="_method" value="DELETE"> <input
      type="submit" value="submit">
  </form>

  <br>
  <br>
  <form action="restTest/restPost" method="post">
    <input type="submit" value="submit">
  </form>
</body>
</html>

注:handler 中 @RequestParam 注解必须加上 required 参数,否则访问页面会出现400错误。

总结

以上就是本文关于springmvc Rest风格介绍及实现代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

SpringMVC入门实例

SpringMVC开发restful API之用户查询代码详解

SpringMVC使用MultipartFile 实现异步上传方法介绍

如有不足之处,欢迎留言指出。

您可能感兴趣的文章:
阅读全文