欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 15:43:22
简介   rest 即 representational state transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准...

简介

  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 rest风格介绍及实现代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

springmvc入门实例

springmvc开发restful api之用户查询代码详解

springmvc使用multipartfile 实现异步上传方法介绍

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

上一篇:

下一篇: