【问题标题】:Show error message while hitting post method url directly in browser直接在浏览器中点击 post 方法 url 时显示错误消息
【发布时间】:2023-03-06 05:31:01
【问题描述】:

我想在使用 java springboot 直接点击 post 方法 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)

【问题讨论】:

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


    【解决方案1】:

    我通过使用@GetMapping 创建相同的重复方法得到了解决方案,它工作正常。现在它重定向到我的自定义错误页面。感谢 yali 通过 cmets 给我的想法。

    @GetMapping("/LoginProcess")
        public String LoginProcess() {
    
            return "err404";
        }
    

    【讨论】:

      【解决方案2】:

      试试@ControllerAdviceExceptionHandler

      @ControllerAdvice
      public class DemoControllerAdvice {
      
          @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
          public ResponseEntity<String> handleIOException(HttpRequestMethodNotSupportedException ex) {
              // prepare responseEntity
              return ResponseEntity.notFound().build();
          }
      
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我是初学者,请给我拦截器代码
        【解决方案4】:

        你不能从浏览器发送http post请求,所以我建议你使用post man工具来测试。

        【讨论】:

        • 我没有创建 rest api,我的问题是如果我直接点击 url localhost:8080/LoginProcess 我需要显示自定义错误,这是我的要求而不是 springboot 默认错误。
        【解决方案5】:

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

        【讨论】:

        • 不,我收到错误 --- 此应用程序没有 /error 的显式映射,因此您将其视为后备。 Sun Mar 03 17:15:22 IST 2019 出现意外错误(类型=未找到,状态=404)。没有可用的消息
        • 尝试使用ajax点击url
        • 无法从 java 中获得?
        • 如果你有 html 或 jsp 页面,在表单标签中使用 method = "POST" 告诉浏览器你有 post 方法要做
        • 如果某些黑客或用户在没有表单页面的情况下直接点击此 url,那么我需要显示自定义错误页面现在明白了吗?
        猜你喜欢
        • 1970-01-01
        • 2018-05-22
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2019-10-23
        • 2010-09-25
        • 1970-01-01
        • 2021-11-23
        相关资源
        最近更新 更多