处理 Spring Boot REST 应用程序的 404 错误时出错 [英] Error while Handling 404 error for Spring Boot REST application

查看:44
本文介绍了处理 Spring Boot REST 应用程序的 404 错误时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I trying my hand out in Spring boot exception handling . I have created a REST application and the application works for all valid url. I am trying to handle the exceptions for invalid url. But if i try hitting the application with an invalid url , i am getting the below exception:-

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 的新手,我无法弄清楚原因.任何指示或建议都会有所帮助.

Being new to spring boot I am not able to figure out the reason. Any pointers or suggestion will be helpful.

想尝试本站提到的选项在此处输入链接描述,一旦我能够删除异常.

Wanted to try out the options mentioned in this siteenter link description here, once i am able to remove the exception.

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

Any pointers on how to handle 404 with annotations in spring 4 will be very helpful.

尝试以下代码:-

@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 添加依赖

Adding the dependency from 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 更新的堆栈跟踪:-

PFB the updated stack trace:-

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 应用注释类:-

The application works fine for valid url mapping. It is build using spring boot. PFB the app annotated classes:-

    @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,我已添加映射以重定向到自定义 html 页面.PFB 为此所做的更改:

I have added the mapping to redirect to custom html pages if a 404 happens. PFB the changes done for this:

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

  • Removed the Annotation @EnableWebMvc from the WebConfig.java class. This is done to remove the error "response already committed", on trying any invalid url.
  • Add the below code in WebConfig.java class and the revenant html pages:

@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);
        }
    };
}

非常感谢您的建议 ans hep.很有用.

Thanks a lot for the suggestions ans hep. It was very useful.

我在完成最初的例外后点击了这个链接

I followed this link after getting through the initial exeception

这篇关于处理 Spring Boot REST 应用程序的 404 错误时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆