我正在处理我的应用程序的登录模块,当我提交`org.springframework.web.HttpRequestMethodNotSupportedException:登录表单时,我遇到了不支持‘POST’的请求方法.不确定如何继续.我也经历过类似的帖子,但这并没有帮助我解决这个问题.
Project Structure个
login.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>
AuthenticationController个
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 ();
}
}
Exception Stacktrace个
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)
我期待着从login.html
检索username
和password
,并对用户进行身份验证,并将其路由到适当的登录页面.
EDIT 1:个
我已经更新了代码,如下所示,删除了AuthenticationController
类,并创建了实现org.springframework.boot.web.servlet.error.ErrorController
和org.springframework.web.servlet.config.annotation.WebMvcConfigurer
的RoutingController
类.
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";
}
}
现在,当我try 提交表单时,这是我在浏览器和控制台上分别遇到的情况.它甚至还没有达到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
EDIT 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 Tester和控制台上得到了预期的响应,如下所示
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
但是,当我try 通过浏览器提交表单时,我仍然遇到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