在浏览器中直接点击发布方法网址时显示错误消息

时间:2019-03-03 11:37:26

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

我想在使用java springboot直接命中方法URL时显示错误消息,所以我使用了try 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)

5 个答案:

答案 0 :(得分:2)

尝试使用,@PostMapping(value = "/loginProcess")或尝试将RequestMapping与方法@RequestMapping(value = "/loginProcess", method = RequestMethod.POST)一起使用

答案 1 :(得分:2)

我通过使用@GetMapping创建相同的重复方法来获得分辨率,并且效果很好。现在它重定向到我的自定义错误页面。感谢yali通过评论使我产生了想法。

@GetMapping("/LoginProcess")
    public String LoginProcess() {

        return "err404";
    }

答案 2 :(得分:1)

尝试if( score <= 100 )public static String scoreToGrade( int score ) { String grade = ""; if( score <= 100 && score >= 90 ) { grade = "A"; } else if( score <= 89 && score >= 80) { grade = "B"; } else if( score <= 79 && score >= 70) { grade = "C"; } else if( score <= 69 && score >= 60) { grade = "D"; } else if( score <= 59 && score >= 50) { grade = "E"; } else if( score <= 49 && score >= 0) { grade = "F"; } else { grade = "Error: score " + score; } return grade; }

@ControllerAdvice

答案 3 :(得分:0)

您无法从浏览器发送http发布请求,因此,我建议您使用发布工具进行测试。

答案 4 :(得分:0)

我不确定Spring是否提供了任何实现。但是您可以通过编写自己的拦截器来处理它。拦截器会在击中控制器之前执行,因此您可以检查将http方法类型转发给错误jsp