SpringMVC实现创建一个支持REST风格的请求处理并解决405

maven的依赖
      <!--导入Spring的核心jar包-->
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.7.RELEASE</version>
      </dependency>

      <!--springWeb的jar-->
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.2.7.RELEASE</version>
      </dependency>

      <!--springMVC的jar-->
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.7.RELEASE</version>
      </dependency>
配置拦截器

web.xml文件中添加一个过滤器

    <!--配置支持rest风格的过滤器-->
    <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>
    
创建表单

对于PUT请求和DELETE请求通过添加隐藏域设置_method属性

<body>
    <a href="hello">hello</a>
    
    <a href="/springMVC/getResources/1">获取资源</a>
    
    <form action="/springMVC/addResources" method="post">
        <input type="submit" value="添加资源"/>
    </form>
    
    <form action="/springMVC/updateResources/1" method="post">
        <input type="hidden" name="_method" value="put"/>
        <input type="submit" value="修改资源"/>
    </form>
    
    <form action="/springMVC/deleteResources/1" method="post">
        <input type="hidden" name="_method" value="delete"/>
        <input type="submit" value="删除资源"/>
    </form>
</body>
编写控制器
package com.xzy.controller;

import org.springframework.web.bind.annotation.*;

@RestController
public class RestfulController {

    @RequestMapping(value = "/getResources/{id}",method = RequestMethod.GET)
    public String getResources(@PathVariable("id") Integer id){
        System.out.println("获取了"+id+"号资源");
        return "success";
    }

    @RequestMapping(value = "/addResources",method = RequestMethod.POST)
    public String addResources(){
        System.out.println("添加了资源");
        return "success";
    }

    @RequestMapping(value = "/updateResources/{id}",method = RequestMethod.PUT)
    public String updateResources(@PathVariable("id") Integer id){
        System.out.println("更新了"+id+"号资源");
        return "success";
    }

    @RequestMapping(value = "/deleteResources/{id}",method = RequestMethod.DELETE)
    public String deleteResources(@PathVariable("id") Integer id){
        System.out.println("删除"+id+"号资源");
        return "success";
    }

/*
    @RequestMapping("/success")
    public String success(){
        return "success";
    }
*/

}

@RestController出现405处理

出现原因:Tomcat7以上对JSP页面要求比较严格,也就是高版本的Tomcat不支持。
在这里插入图片描述

方式一

将@Controller换成@RestController注解

@RestController
public class RestfulController {
方式二

编写一个控制器进行重定向跳转

    @RequestMapping(value = "/updateResources/{id}",method = RequestMethod.PUT)
    public String updateResources(@PathVariable("id") Integer id){
        System.out.println("更新了"+id+"号资源");
        return "redirect:/success";
    }

    @RequestMapping(value = "/deleteResources/{id}",method = RequestMethod.DELETE)
    public String deleteResources(@PathVariable("id") Integer id){
        System.out.println("删除"+id+"号资源");
        return "redirect:/success";
    }

    @RequestMapping("/success")
    public String success(){
        return "success";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值