我想在使用Java Springboot直接击中POST方法URL时显示错误消息,因此我使用了尝试catch异常,但是它不起作用,我收到的错误消息,我在这个问题中提到了这些错误消息,对帮助或建议表示赞赏。
我只需要击中localhost:8080/loginprocess,url,我需要显示error404 html页面。
控制器方法
@PostMapping("/LoginProcess")
public String LoginProcess(@ModelAttribute("customer") Customer thecustomer,HttpSession session) {
try {
Customer result = customerservice.Login_service(thecustomer.getUserName(),thecustomer.getPassword());
if(result==null)
{
return "login";
}
else if(result.getRole().equals("1"))
{
session.setAttribute("l_uname",result.getUserName() );
session.setAttribute("l_pwd",result.getPassword() );
return "admindash";
}
else
{
session.setAttribute("l_uname",result.getUserName() );
session.setAttribute("l_pwd",result.getPassword() );
return "customerdash";
}
}catch(Exception e) {
return "err404";
}
}
错误消息
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Mar 03 16:18:46 IST 2019
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:200)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:422)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:368)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:65)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:401)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1231)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1014)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
尝试使用, @PostMapping(value = "/loginProcess")
或尝试使用方法 @RequestMapping(value = "/loginProcess", method = RequestMethod.POST)
我通过使用@getMapping创建相同的重复方法来获得分辨率,并且效果很好。现在,它将其重定向到我的自定义错误页面。感谢Yali通过评论给我的想法。
@GetMapping("/LoginProcess")
public String LoginProcess() {
return "err404";
}
尝试@ControllerAdvice
和ExceptionHandler
@ControllerAdvice
public class DemoControllerAdvice {
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<String> handleIOException(HttpRequestMethodNotSupportedException ex) {
// prepare responseEntity
return ResponseEntity.notFound().build();
}
}
您无法从浏览器发送HTTP POST请求,因此我建议您使用Post Man工具进行测试。
我不确定春季是否提供了任何实现。但是,您可以通过编写自己的互助者来处理它。打印机将在击中控制器之前执行,因此您可以检查HTTP方法将其转发为ERROR JSP