SpringMVC implementation creates a REST-style request processing and resolves 405

maven dependencies

      <!--导入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>

Configure interceptors

Add a filter to the web.xml file

    <!--配置支持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>
    

create form

For PUT requests and DELETE requests set the _method attribute by adding a hidden field

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

Write the controller

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

}

405 processing occurs in @RestController

Cause: Tomcat7 and above have strict requirements on JSP pages, that is, Tomcat of higher versions does not support it.
insert image description here

method one

Replace @Controller with @RestController annotation

@RestController
public class RestfulController {
    
    
way two

Write a controller for redirection jump

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

Guess you like

Origin blog.csdn.net/weixin_42643321/article/details/107649187
405