Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理Spring引导REST应用程序的404错误时出错_Java_Spring Mvc_Spring Boot_Spring 4 - Fatal编程技术网

Java 处理Spring引导REST应用程序的404错误时出错

Java 处理Spring引导REST应用程序的404错误时出错,java,spring-mvc,spring-boot,spring-4,Java,Spring Mvc,Spring Boot,Spring 4,我正在尝试我的手在春季启动异常处理。 我已经创建了一个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

我正在尝试我的手在春季启动异常处理。 我已经创建了一个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]
作为一个新手,我无法找出原因。任何提示或建议都会有所帮助

想要尝试在这个网站上提到的选项,一旦我能够删除异常

任何关于如何在Spring4中使用注释处理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>
]

对于有效的url映射,应用程序运行良好。它是使用spring boot构建的。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);
    }   

}
@EnableJpaRepositories
@启用自动配置
公共类AppConfig{
公共静态void main(字符串[]args){
run(AppConfig.class,args);
}
@配置
@EnableWebMvc
公共类WebApplicationConfig扩展了WebMVCConfigureAdapter{
@凌驾
公共无效配置MessageConverters(

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


升级到Spring Boot 1.1.5.版本。

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

  • 已从中删除注释@EnableWebMvc 这样做是为了删除错误“响应” 已提交”,尝试任何无效url时
  • 在WebConfig.java类和返回的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);
            }
        };
    }
    
非常感谢ans hep的建议。它非常有用


在完成初始执行后,我跟随了此链接

我无法跟随链接中的步骤,因为我在尝试未知URL时遇到了上述错误。我在问题本身中给出了与我的参考相同的链接。如果有关于异常的任何信息,请让我知道。请使用最新版本的Anks Dave Syer作为参考建议。使用最新版本的spring boot。我已经从pom.xml添加了spring boot依赖项。但是运气不好…相同的错误堆栈跟踪仍然显示为1.1.4。如果你还没有更新,你也可以更新吗?是的,Dave,我忘了更新堆栈跟踪。我现在已经添加了。它对我来说适用于一个简单的404。也许是时候分享整个了项目和一些复制步骤?我有些工作要做,但还没有完全完成。在删除@EnableWebMvc之后,我没有收到错误。我正在进入默认的白色标记页。你需要打开一点。是否有堆栈跟踪?
@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);
        }
    };
}