java - 在浏览器中直接点击 post 方法 url 时显示错误消息

标签 java spring spring-boot spring-mvc jakarta-ee

我想在使用 java springboot 直接点击 post 方法 url 时显示错误消息,所以我使用了 try catch exception 但它不起作用,我收到了我在这个问题中提到的错误消息,感谢帮助或建议。

我只是要求当我们访问 localhost:8080/LoginProcess 时,我需要显示 error404 html 页面的 url。

Controller 方法

@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 与方法一起使用 @RequestMapping(value = "/loginProcess", method = RequestMethod.POST)

关于java - 在浏览器中直接点击 post 方法 url 时显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54968395/

相关文章:

java - mvn 测试期间 System.console() 在命令行中返回 null

java - 为什么波浪号和插入符字符不被 java ReplaceAll 替换

java - java中vlcj播放视频失败

java - 访问模型中的 DAO - Spring Data MongoDB

java - Spring IoC - 依赖注入(inject) -> NullPointerException

java - Spring启动Thymeleaf : How do I display an entity name from the database and make that name a hyperlink?

java - spring boot如何在目标中创建jar文件而无需在pom中添加插件

java - Spring Boot 应用程序与部署在 Tomcat/Jetty 上的 .war 文件

java - 如何随机调用方法(Java JDK11)?

java - 使用 CrudRepository 只提取某些字段?