处理Spring Boot REST应用程序的404错误时出错

时间:2014-09-01 12:13:49

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

我在Spring启动异常处理中尝试了一下。 我创建了一个REST应用程序,该应用程序适用于所有有效的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]

刚开始春季靴子,我无法弄清楚原因。任何指针或建议都会有所帮助。

一旦我能够删除该例外,我们就想试试这个网站enter link description here中提到的选项。

有关如何在春季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

该应用程序适用于有效的网址映射。它是使用弹簧靴构建的。 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);
    }   

}

3 个答案:

答案 0 :(得分:1)

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

  • 从中移除了注释@EnableWebMvc 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);
            }
        };
    }
    

非常感谢你提出的建议。这非常有用。

在完成初始执行后我跟着这个链接

答案 1 :(得分:0)

您可以按照以下博客来处理mvc异常处理

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

答案 2 :(得分:0)

升级到Spring Boot 1.1.5.RELEASE。