spring中的restful风格的简单使用

REST全称是Representational State Transfer,中文意思是表征性状态转移。REST就是一种设计API的模式。最常用的数据格式是JSON。由于JSON能直接被JavaScript读取,所以,以JSON格式编写的REST风格的API具有简单、易读、易用的特点。

常用的请求方式如下

请求方式含义
get获取数据
post增加数据
put修改数据
delete删除数据

我们平时常用的提交方式,一是get ,二是 post

在这里插入图片描述
form表单中也只有两种请求方式,但是springmvc对 restful 风格有良好的支持,下面看看sprigmvc中怎么使用 restful 风格吧。

在配置文件中添加拦截器

    <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>

再在提交方式为post的form表单中添加字段_method,value为提交的方式

<form action="book/1" method="post">
    <input name="_method" value="delete"/>
    <input type="submit" value="ok"/>
</form>

为什么要这么做呢,为甚么要添加_method 呢?
进入HiddenHttpMethodFilter一探究竟

 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest requestToUse = request;
        //判断表单的提交方式是否为post
        if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
        	//获取参数,methodParam = "_method"
            String paramValue = request.getParameter(this.methodParam);
            //判断是否为null
            if (StringUtils.hasLength(paramValue)) {
                String method = paramValue.toUpperCase(Locale.ENGLISH);
                //是否包含一下几种方式
                if (ALLOWED_METHODS.contains(method)) {
                    requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
                }
            }
        }
		//放行
        filterChain.doFilter((ServletRequest)requestToUse, response);
    }
public class HiddenHttpMethodFilter extends OncePerRequestFilter {
    private static final List<String> ALLOWED_METHODS;
    public static final String DEFAULT_METHOD_PARAM = "_method";
    private String methodParam = "_method";

支持的提交方式,put 、delete、 patch

static {
        ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));
    }

具体的使用如下

<a href="book/1">get查看图书</a>

<form action="book/1" method="post">
    <input type="submit" value="ok"/>
</form>

//表单多了input
<form action="book/1" method="post">
    <input name="_method" value="delete"/>
    <input type="submit" value="ok"/>
</form>

<form action="book/1" method="post">
    <input name="_method" value="put"/>
    <input type="submit" value="ok"/>
</form>

请求路径也发生了变化

不使用restful风格使用restful风格
book?bookid = 1book/1

那怎么获取参数呢?这个变量值在路径上,也是一个资源占位符,可以通过@RequestMapping(value = “/book/{bid}”,method = RequestMethod.GET) 来接收,/book/{bid}中的{ }表示占位符,然后通过 @PathVariable(“bid”) 接受这个值

注意:{bid }里的变量值必须和 @PathVariable(“bid”) 对应

    @RequestMapping(value = "/book/{bid}",method = RequestMethod.GET)
    public String getBook(@PathVariable("bid") Integer id){
        System.out.println("get...");
        return "hello";
    }
    @RequestMapping(value = "/book/{bid}",method = RequestMethod.PUT)
    public String Putbook(@PathVariable("bid") Integer id){
        System.out.println("put...");
        return "hello";
    }
    @RequestMapping(value = "/book/{bid}",method = RequestMethod.DELETE)
    public String Deletebook(@PathVariable("bid") Integer id){
        System.out.println("delete...");
        return "hello";
    }
    @RequestMapping(value = "/book/{bid}",method = RequestMethod.POST)
    public String postbook(@PathVariable("bid") Integer id){
        System.out.println("post...");
        return "hello";
    }

如果你的tomcat的版本是8以上,可能会出现 405

在这里插入图片描述
解决方案一

在跳转的界面添加 isErrorPage=“true”

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>

解决方案二

在方法上添加 @ResponseBody 注解

	 @ResponseBody
    @RequestMapping(value = "/book/{bid}",method = RequestMethod.PUT)
    public String Putbook(@PathVariable("bid") Integer id){
        System.out.println("put...");
        return "hello";
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗罗的1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值