Skip to content

Avoid 406 Not Acceptable for error pages #19522

Closed
@bclozel

Description

@bclozel

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions