profile
viewpoint

Ask questionsErrorPageFilter ignores REST response in case of Exception

I'm running a Spring Boot web app with Thymeleaf + Spring MVC with a mix of MVC and REST calls (from JS code). In a @ControllerAdvice I have the following exception handling code :

@ExceptionHandler(MyRuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody MyRestResponse handleMyRuntimeException(MyRuntimeException exception) {
    return new MyRestResponse("Some data I want to send back to the client.");
}

Running Spring Boot with Tomcat embedded container I receive a MyRestResponse object in my ajax failure handler as expected but when I deploy in standalone Tomcat the response is ignored as the ErrorPageFilter forward the exception info instead due to the following code in doFilter() :

if (status >= 400) {
    handleErrorStatus(request, response, status, wrapped.getMessage());
    response.flushBuffer();
}

I went around the problem by writing directly the response to the output stream.

@ExceptionHandler(RuntimeException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public void handleRuntimeException(HttpServletRequest request, HttpServletResponse response, RuntimeException exception) {
        try {
            jacksonMessageConverter.write(new MyRestResult(translateMessage(exception)), MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
            response.flushBuffer(); // Flush to commit the response
            } catch (IOException e) {
            e.printStackTrace();
        }
    }

That solution is ok for me but I think that should be possible to return a @ResponseBody in case of an error happening during a REST call. I have also tried to return a ResponseEntity as suggested by @rstoyanchev here but still see the same issue.

spring-projects/spring-boot

Answer questions pariaSadatHosseiny

@pariaSadatHosseiny In a logback.xml or logback-spring.xml file. You could also configure it in application.properties using logging.level.org.springframework.boot.context.web.ErrorPageFilter=off

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

thank you for Your Answer .I have such kind of Error but i am thinking if use this code just i dont see the Exception in the Log but I dont Understand the Reason,isn't it?!

my Error Is here :

2019-07-14 22:40:33 99839 [qtp1599771323-21] ERROR o.s.b.context.web.ErrorPageFilter - Cannot forward to error page for request [/vendor/jQuery/jquery.js] 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 org.eclipse.jetty.io.EofException: null at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:284)

useful!

Related questions

Dependency resolution fails with Gradle 5.3.x to 5.6.x
[spring-boot 2.3.0] ResponseStatusException reason is ignored - spring-boot hot 3
get error messag Could not fetch the SequenceInformation from the database, hibernate_sequence doesn't exist hot 2
Controller annotated with @Timed and active TimedAspect clashes with WebMvcMetricsFilter hot 2
Source file must be provided failure when running spring-boot:repackage from the command-line hot 2
Actuator: NPE in LongTaskTimingHandlerInterceptor hot 2
ConfigurationProperties with constructor binding cannot be mocked hot 2
NoClassDefFoundError: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver hot 2
No bean named 'entityManagerFactory' available hot 2
Bug in org.springframework.boot on 2.2.3.RELEASE version hot 2
"factory already defined" error with spring-boot-maven-plugin in maven multi-module application hot 2
Deadlock between BackgroundPreinitializer and main thread in Spring Cloud Config Server hot 2
spring boot Servlet.service() for servlet [dispatcherServlet] in context with path threw exception hot 2
Migrating OAuth2 from Spring Boot 1.5 to 2.0 Broken hot 2
Add AutoConfiguration support for spring-security-saml2-service-provider hot 2
Github User Rank List