【问题标题】:Error while Handling 404 error for Spring Boot REST application处理 Spring Boot REST 应用程序的 404 错误时出错
【发布时间】:2014-10-25 16:05:00
【问题描述】:

我在 Spring boot 异常处理中尝试了我的手。 我创建了一个 REST 应用程序,该应用程序适用于所有有效的 url。 我正在尝试处理无效 url 的异常。 但是,如果我尝试使用无效的 url 访问应用程序,我会收到以下异常:-

13:04:02.940 [http-bio-8081-exec-3] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 44 ms
13:04:03.177 [http-bio-8081-exec-3] ERROR o.s.boot.context.web.ErrorPageFilter - Cannot forward to error page for/sample/processgetMessage5 (response is committed), so this response may have the wrong status code
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:348) ~[catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338) ~[catalina.jar:7.0.55]
    at org.springframework.boot.context.web.ErrorPageFilter.handleErrorStatus(ErrorPageFilter.java:123) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:104) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:89) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.55]

作为 Spring Boot 的新手,我无法弄清楚原因。任何指示或建议都会有所帮助。

想试试这个站点enter link description here 中提到的选项,一旦我能够消除异常。

任何关于如何在 spring 4 中使用注释处理 404 的指针都会非常有帮助。

试试下面的代码:-

@Configuration
@EnableWebMvc
@EnableSwagger
public class WebConfig extends WebMvcConfigurerAdapter {
.......

  @Bean
            public EmbeddedServletContainerCustomizer containerCustomizer() {

                return new EmbeddedServletContainerCustomizer() {
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {

                    ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                    ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/401.html");
                    ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/401.html");

                    container.addErrorPages(error401Page, error404Page, error500Page);
                }
            };
        }   

从 pom.xml 添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.1.5.RELEASE</version>            
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.1.5.RELEASE</version>            
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.1.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

PFB 更新的堆栈跟踪:-

22:09:03.210 [http-bio-8081-exec-3] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 28 ms
22:09:03.413 [http-bio-8081-exec-3] ERROR o.s.boot.context.web.ErrorPageFilter - Cannot forward to error page for/applicationurl/processMessage11 (response is committed), so this response may have the wrong status code
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:348) ~[catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338) ~[catalina.jar:7.0.55]
    at org.springframework.boot.context.web.ErrorPageFilter.handleErrorStatus(ErrorPageFilter.java:134) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:111) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:58) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:87) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:100) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.55]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.55]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.55]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [catalina.jar:7.0.55

]

该应用程序适用于有效的 url 映射。它是使用弹簧靴构建的。 PFB 应用注释类:-

    @EnableJpaRepositories
    @EnableAutoConfiguration
    public class AppConfig {


        public static void main(String[] args) {
            SpringApplication.run(AppConfig .class, args);
        }



@Configuration
@EnableWebMvc
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        converters.add(mappingJackson2HttpMessageConverter);
        converters.add(new StringHttpMessageConverter());

        super.configureMessageConverters(converters);
    }

public class WebApplicationXML extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }   

}

【问题讨论】:

  • 一个简单的 404 对我有用。也许是时候分享整个项目和一些重现的步骤了?
  • 我有一些工作要做,虽然不完全。删除@EnableWebMvc 后,我没有收到错误消息。我正在转发到默认的 Whitemarker 页面。
  • 你需要打开一点。有堆栈跟踪吗?

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


【解决方案1】:

如果发生 404,我已添加映射以重定向到自定义 html 页面。 PFB 为此所做的更改:

  • 从 WebConfig.java 类。这样做是为了消除错误“响应 已经提交”,在尝试任何无效的网址时。
  • 在 WebConfig.java 类和 revenant html 页面中添加以下代码:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
    
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
    
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED,
                        "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
                        "/404.html");
                ErrorPage error500Page = new ErrorPage(
                        HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
                ErrorPage error505Page = new ErrorPage(
                        HttpStatus.HTTP_VERSION_NOT_SUPPORTED, "/505.html");
                ErrorPage error506Page = new ErrorPage(
                        HttpStatus.METHOD_NOT_ALLOWED, "/405.html");
                container.addErrorPages(error401Page, error404Page,
                        error500Page, error505Page, error506Page);
            }
        };
    }
    

非常感谢您的建议和建议。这非常有用。

在完成初始异常后,我点击了此链接

【讨论】:

    【解决方案2】:

    你可以按照下面的博客来处理mvc异常处理

    http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

    【讨论】:

    • 我无法按照链接中的步骤操作,因为当我尝试使用未知 URL 时出现上述错误。
    • 我在问题本身中给出了相同的链接作为我的参考。如果有任何关于异常的信息,请告诉我。
    【解决方案3】:

    升级到 Spring Boot 1.1.5.RELEASE。

    【讨论】:

    • 使用最新版本
    • 感谢 Dave Syer 的建议。使用最新版本的spring boot。我已经从pom.xml添加了spring boot依赖。但是没有运气...同样的错误
    • 堆栈跟踪仍然显示 1.1.4。如果您还没有更新,您也可以更新吗?
    • 是的,戴夫,我忘记更新堆栈跟踪了。我现在已经添加了。
    猜你喜欢
    • 2021-12-18
    • 2017-07-28
    • 2020-10-24
    • 1970-01-01
    • 2015-09-04
    • 2015-08-10
    • 2018-07-18
    • 2017-09-18
    • 2018-08-24
    相关资源
    最近更新 更多