sprimgmvc实现rest风格,返回总是405,但是接口又访问到了咋整?

沉梦昂志
  • 6

问题描述

看了 rest风格的增删改查 视屏后,我照着视屏中的内容实现rest风格的接口,但是雷丰阳在视屏里,表单中调用完接口后是能正确转发到其他界面的,但是我就一直是405,我的接口中的语句输出了,也就是接口访问到了,但是网页跳转不了,get/post/put/delete 四种请求方式只有get能正确跳转网页(四种返回的都是一样的地址),请问怎么解决呢??

相关代码

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!-- 整合SpringMVC -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
    <!-- 通过配置HiddenHttpMethodFilter实现PUT、DELETE等请求 -->
    <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>
</web-app>
  • springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="dao,service,controller"/>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/**" location="/html/"/>
</beans>
  • RestController.java
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Surrin1999
 */
@Controller
@RequestMapping("/part1")
public class RestController {

    @RequestMapping(value = "/book/{id}", method = RequestMethod.POST)
    public String addBook(@PathVariable String id) {
        System.out.println("正在增加图书..." + "id:" + id);
        return "/success.html";
    }

    @RequestMapping(value = "/book/{id}", method = RequestMethod.DELETE)
    public String deleteBook(@PathVariable String id) {
        System.out.println("正在删除图书..." + "id:" + id);
        return "/success.html";
    }

    @RequestMapping(value = "/book/{id}/{name}", method = RequestMethod.PUT)
    public String updateBook(@PathVariable String id, @PathVariable String name) {
        System.out.println("正在更新图书..." + "id:" + id + ",name:" + name);
        return "/success.html";
    }

    @RequestMapping(value = "/book/{id}", method = RequestMethod.GET)
    public String selectBook(@PathVariable String id) {
        System.out.println("正在查找图书..." + "id:" + id);
        return "/success.html";
    }
}
  • form.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/part1/book/1" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input value="更新" type="submit">
</form>
</body>
</html>
  • 包结构图
    包结构图
  • 测试图片

表单
405
接口可以访问,但是不能页面跳转

  • 上图中的警告信息如下

    20-May-2021 22:31:31.632 警告 [http-nio-8080-exec-5] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.logException Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported]

2021-5-21 更新

补充一下,代码已放到https://gitee.com/surrin1999/...
以及我用的Tomcat版本是9.0.24

回复
阅读 1k
1 个回答
✓ 已被采纳

HiddenHttpMethodFilter 放在dispathcer前面





tomcat - 8.5.31 完全没有任何问题


2021-5-22 补充回答
https://blog.csdn.net/perfect...
我针对于这个问题的解决方案

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏