org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”

问题描述 投票:0回答:1

我正在开发我的

Java
Springboot
React
Thymeleaf
应用程序的登录模块,并且遇到 `org.springframework.web.HttpRequestMethodNotSupportedException:提交时不支持请求方法“POST” HTML 登录表单。不知道如何继续。我已经浏览过类似的帖子,但它并没有帮助我解决问题。

项目结构

登录.html

<!--<div th:if="${param.error}">Invalid username and/or password.</div>-->
<form action="#" th:action="@{/validateLogin}" method="post" th:object="${userInfo}" class="row g-4">
   <div class="col-12">
      <label>Username<span class="text-danger">*</span></label>
      <div class="input-group">
         <div class="input-group-text"><i class="bi bi-person-fill"></i></div>
         <label>
         <input type="text" class="form-control" placeholder="Enter Username" th:field="*{username}">
         </label>
      </div>
   </div>
   <div class="col-12">
      <label>Password<span class="text-danger">*</span></label>
      <div class="input-group">
         <div class="input-group-text"><i class="bi bi-lock-fill"></i></div>
         <label>
         <input type="text" class="form-control" placeholder="Enter Password" th:field="*{password}">
         </label>
      </div>
   </div>
   <div class="col-sm-6">
      <div class="form-check">
         <input class="form-check-input" type="checkbox" id="inlineFormCheck">
         <label class="form-check-label" for="inlineFormCheck">Remember me</label>
      </div>
   </div>
   <div class="col-sm-6">
      <a href="#" class="float-end text-primary">Forgot Password?</a>
   </div>
   <div class="col-12">
      <button type="submit" class="btn btn-primary px-4 float-end mt-4">login</button>
   </div>
</form>

身份验证控制器

import in.sandeep.campusconvene.model.Users;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class AuthenticationController {

    @RequestMapping(path = "/validateLogin", method = RequestMethod.POST)
    public String validateLogin(@ModelAttribute Users userInfo){
        return userInfo.getUsername ();
    }
}

异常堆栈跟踪

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Oct 13 09:15:25 IST 2023
There was an unexpected error (type=Method Not Allowed, status=405).
Method 'POST' is not supported.
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' is not supported
    at org.springframework.web.servlet.support.WebContentGenerator.checkRequest(WebContentGenerator.java:381)
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:164)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:51)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:894)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1740)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:833)
 

我希望从

username
检索
password
login.html
并验证用户身份并将其路由到适当的登陆页面。

编辑1:

我更新了代码如下,删除了

AuthenticationController
类并创建了一个实现
RoutingController
org.springframework.boot.web.servlet.error.ErrorController
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
类。

import in.sandeep.campusconvene.model.Users;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@RestController
public class RoutingController implements ErrorController, WebMvcConfigurer {

    private static final String MAIN_APPLICATION_PATH = "/";
    private static final String LOGIN_VALIDATOR_PATH = "/validateLogin";
    private static final String ERROR_PATH = "/error";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(MAIN_APPLICATION_PATH)
                .setViewName("forward:/login.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @RequestMapping(value = ERROR_PATH)
    public String handleError(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
        return String.format("<html><body><h2>Error Page</h2><div>Status code: <b>%s</b></div>" +
                        "<div>Exception Message: <b>%s</b></div><body></html>",
                statusCode, exception == null ? "N/A" : exception.getMessage());
    }

    @RequestMapping(value = LOGIN_VALIDATOR_PATH, method = RequestMethod.POST)
    @ResponseStatus(value= HttpStatus.OK)
    public String validateLogin(@RequestBody Users users, Model model){
        System.out.println ("INSIDE validateLogin.....");
        return "LET'S AUTHENTICATE";
    }
}

现在,当我尝试提交表单时,这分别是我在浏览器和控制台上遇到的情况。连

validateLogin
方法都没有达到。

需要有关如何排序的帮助。

2023-10-14T12:17:15.890+05:30 DEBUG 10130 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : POST "/", parameters={masked}
2023-10-14T12:17:15.891+05:30 DEBUG 10130 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ParameterizableViewController [view="forward:/login.html"]
2023-10-14T12:17:15.901+05:30  WARN 10130 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' is not supported]
2023-10-14T12:17:15.901+05:30 DEBUG 10130 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED
2023-10-14T12:17:15.902+05:30 DEBUG 10130 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for POST "/error", parameters={masked}
2023-10-14T12:17:15.904+05:30 DEBUG 10130 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to in.sandeep.campusconvene.controller.RoutingController#handleError(HttpServletRequest)
2023-10-14T12:17:15.927+05:30 DEBUG 10130 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, */*, application/json, application/*+json]
2023-10-14T12:17:15.928+05:30 DEBUG 10130 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing ["<html><body><h2>Error Page</h2><div>Status code: <b>null</b></div><div>Exception Message: <b>N/A</b> (truncated)..."]
2023-10-14T12:17:15.930+05:30 DEBUG 10130 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 405

编辑2:

使用当前版本的

validateLogin
方法

@RequestMapping(value = LOGIN_VALIDATOR_PATH, method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public String validateLogin(HttpServletRequest request) {
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  return username + password;
}

我现在在 API 测试器以及控制台上得到了预期的响应,如下所示

2023-10-14T23:24:50.091+05:30 DEBUG 7663 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : POST "/validateLogin", parameters={masked}
2023-10-14T23:24:50.091+05:30 DEBUG 7663 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to in.sandeep.campusconvene.controller.RoutingController#validateLogin(HttpServletRequest)
2023-10-14T23:24:50.093+05:30 DEBUG 7663 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, application/json, application/*+json]
2023-10-14T23:24:50.093+05:30 DEBUG 7663 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing ["user1 foo"]
2023-10-14T23:24:50.094+05:30 DEBUG 7663 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

但是当我尝试通过浏览器提交表单时,我仍然遇到

405 METHOD_NOT_ALLOWED

2023-10-14T23:28:30.723+05:30 DEBUG 7663 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : POST "/", parameters={masked}
2023-10-14T23:28:30.724+05:30 DEBUG 7663 --- [io-8080-exec-10] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ParameterizableViewController [view="forward:/login.html"]
2023-10-14T23:28:30.724+05:30  WARN 7663 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' is not supported]
2023-10-14T23:28:30.724+05:30 DEBUG 7663 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED
2023-10-14T23:28:30.725+05:30 DEBUG 7663 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for POST "/error", parameters={masked}
2023-10-14T23:28:30.725+05:30 DEBUG 7663 --- [io-8080-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to in.sandeep.campusconvene.controller.RoutingController#handleError(HttpServletRequest)
2023-10-14T23:28:30.727+05:30 DEBUG 7663 --- [io-8080-exec-10] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, */*, application/json, application/*+json]
2023-10-14T23:28:30.728+05:30 DEBUG 7663 --- [io-8080-exec-10] m.m.a.RequestResponseBodyMethodProcessor : Writing ["<html><body><h2>Error Page</h2><div>Status code: <b>null</b></div><div>Exception Message: <b>N/A</b> (truncated)..."]
2023-10-14T23:28:30.729+05:30 DEBUG 7663 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 405
java spring-boot thymeleaf
1个回答
0
投票

我已经发现问题了。我见过很多问题。

不要直接访问logic.html

首先,你的 logic.html 应该位于

/templates
目录中,而不是位于
/static
目录中。

因为 Thymeleaf 默认情况下仅检查

.html
目录中的
/templates
文件。

我已经修复了你的 login.html:

<!--<div th:if="${param.error}">Invalid username and/or password.</div>-->
<form th:action="@{/validateLogin}" method="post" th:object="${userInfo}" class="row g-4">
   <div class="col-12">
      <label>Username<span class="text-danger">*</span></label>
      <div class="input-group">
         <div class="input-group-text"><i class="bi bi-person-fill"></i></div>
         <label>
         <input type="text" class="form-control" placeholder="Enter Username" th:field="*{userName}">
         </label>
      </div>
   </div>
   <div class="col-12">
      <label>Password<span class="text-danger">*</span></label>
      <div class="input-group">
         <div class="input-group-text"><i class="bi bi-lock-fill"></i></div>
         <label>
         <input type="text" class="form-control" placeholder="Enter Password" th:field="*{password}">
         </label>
      </div>
   </div>
   <div class="col-sm-6">
      <div class="form-check">
         <input class="form-check-input" type="checkbox" id="inlineFormCheck">
         <label class="form-check-label" for="inlineFormCheck">Remember me</label>
      </div>
   </div>
   <div class="col-sm-6">
      <a href="#" class="float-end text-primary">Forgot Password?</a>
   </div>
   <div class="col-12">
      <button type="submit" class="btn btn-primary px-4 float-end mt-4">login</button>
   </div>
</form>

action=#
标签中删除不必要的
<form>

我根据您提供的信息准备了一个用户课程。

public class Users {
    
    private String userName;
    private String password;

    // constructor, getter and setters
}

为了显示 validateLogin 成功,我也创建了一个 success.html。

<body>
    <p>User Name: <strong th:text="${userInfo.userName}"></strong></p>
    <p>Password: <strong th:text="${userInfo.password}"></strong></p>
</body>

将控制器更新为如下所示:

@Controller
public class AuthenticationController {
    
    @GetMapping("/login-page")
    public String loginPage(Model model){
        model.addAttribute("userInfo", new Users());
        return "login.html";
    }

    @PostMapping("validateLogin")
    public ModelAndView validateLogin(@ModelAttribute Users userInfo){
       ModelAndView view = new ModelAndView();
       view.setViewName("success");
       view.addObject("userInfo", userInfo);
       return view;
    }
}

注意: 您必须创建一个空的

Users
对象并将其通过模型传递到 thymeleaf,以便它可以流行您发送的数据。否则,它甚至不会命中 POST 端点。

输出:

http://localhost:8080/登录页面

单击“提交”按钮。它会打电话给

http://localhost:8080/validateLogin

这会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.