I've made a small Spring Boot application and now I'm trying to add my own exception handling. I'm having a problem where I get an error in the log even though the app works as expected. The configuration:
The exception handler looks as follows:
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
@ResponseBody ErrorInfo handleNotFoundRequest(HttpServletRequest req, Exception ex) {
return new ErrorInfo(req.getRequestURL().toString(), ex);
}
}
My controller that throws the exception:
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = {"application/json"})
public @ResponseBody
HashMap environment(@PathVariable("id") long id) {
HashMap<String, Object> map = new HashMap();
Environment env = environmentService.getEnvironment(id);
if(env == null) throw new NotFoundException("Environment not found: " + id);
map.put("environment", env);
return map;
}
My Spring Boot application setup:
@SpringBootApplication
@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)
@ComponentScan
public class QaApiApplication {
public static void main(String[] args) {
SpringApplication.run(QaApiApplication.class, args);
}
}
ServletInitializer:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(QaApiApplication.class);
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
......
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
If I do a call that generates the exception I get the correct response, a 404 with the expected JSON. I do however see a ErrorPageFilter error in the log:
2015-04-09 21:05:38.647 ERROR 85940 --- [io-8080-exec-16] o.s.boot.context.web.ErrorPageFilter: Cannot forward to error page for request [/environments/1] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
There is no errors on startup / deployment and everything seem to be working as far as I can see. I'm suspecting that there is some default behavior that I've not overridden correctly or so, but I've yet to figure out exactly what.
Any help / tip around this would be greatly appreciated as I've become stuck on what the problem could be.
If you still uses Spring-boot version 1.2.3 (not 1.4.2.RELEASE where solution provided) then extend SpringBootServletInitializer
and override method run
as following:
@SpringBootApplication
@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)
@ComponentScan
public class QaApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(QaApiApplication.class, args);
}
@Override
protected WebApplicationContext run(SpringApplication application) {
application.getSources().remove(ErrorPageFilter.class);
return super.run(application);
}
}
It works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With