我在Spring引导异常处理中试图伸出援手。 我已经创建了一个REST应用程序,并且该应用程序适用于所有有效的url。 我想处理无效网址的例外情况。 但如果我尝试打网址无效的申请,我得到以下异常: -为Spring Boot REST应用程序处理404错误时出错
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,一旦我能够删除异常。
任何关于如何在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也许是时候分享整个项目和一些步骤来重现后此链接? – 2014-09-02 13:58:43
我有一些工作,但不完全。删除@EnableWebMvc后,我没有收到错误。我得到了默认的Whitemarker页面。 – Manu 2014-09-04 11:48:34
你需要打开一下。是否有堆栈跟踪? – 2014-09-04 21:09:58