Given a Spring MVC Controller like:

@RestController
public class TestController {
    @GetMapping(path = "/test", produces = "text/plain")
    public String test() {
        throw new ExpectedException();
    }

    @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Expected!")
    static class ExpectedException extends RuntimeException {

    }
}

When clients send requests to the "/test" endpoint and the Accept: text/plain request header, the error response rendered by Spring Boot's ErrorController has an empty body and a "406 Not Acceptable" HTTP status. Although the client is not able to read the response body, we should not hide the original HTTP response status as it might give important information about the error.

This happens because the ErrorController tries to render the error map with the requested media type, which is text/plain. Since it is not possible to serialize the error map in that format, the original HTTP status response is overridden and the client gets a 406 instead.

In these cases, we should not write the response body and instead use the orignal HTTP response status with an empty response body.

We've seen instances of this problem in #13163 and spring-projects/spring-framework#23421

Comment From: snicoll

@bclozel would it be possible to backport this one?

Comment From: bclozel

@snicoll I'm not sure. This is improving the error handling support but it changes the existing behavior which is not strictly a bug. So I'm wondering if developers are expecting this somehow in tests, or (in a strange way) relying on this.

Comment From: rstoyanchev

I think there is a good case for a backport. It seems unlikely that an application can do something meaningful that would depend on the current behavior. A test might be fail somewhere but that's an easy change.

Comment From: rstoyanchev

It would be really useful to have this one in 2.2.x. I keep running into issues where the original status is obscured.

Comment From: wilkinsona

It was back ported in https://github.com/spring-projects/spring-boot/commit/2f78c72f920f67763174c88f897dc269277d8ca1 which was in 2.2.3. Assuming that you're seeing the problem with 2.2.3 or later, it sounds like we need to investigate to learn why you're still seeing a problem. @rstoyanchev is there a sample project somewhere that we can look at?

Comment From: rstoyanchev

Ah yes indeed it was, and it is working as expected, my bad. Cheers!

Comment From: mojtaba-ghasemi

in spring boot: imagine that we want to return in web service method same as:

ResponseEntity.status(HttpStatus.CREATED)
        .body(myObject);

myObject class need to use @Data annotation from Lombok package

@Data
public class myClass{
...
}