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 はそれをサポートしていません。
ここに画像の説明を挿入

方法 1

@Controller を @RestController アノテーションに置き換えます

@RestController
public class RestfulController {
    
    
方法2

リダイレクトジャンプ用のコントローラーを作成する

    @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