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";
    }

猜你喜欢

转载自blog.csdn.net/weixin_42643321/article/details/107649187
今日推荐