Skip to content

BasicErrorController returns body even if NO_CONTENT status causing 406 later #18136

@aleksanderlech

Description

@aleksanderlech

Having exception defined like this:

@ResponseStatus(HttpStatus.NO_CONTENT)
public class NoContentException extends RuntimeException {

    public NoContentException(String id) {
        super("ID " + id + " has no content.");
    }
}

And the controller method as following:

    @GetMapping(path = "/{id}/content", produces = {
            "text/plain",
            "audio/wav",
            "image/tiff"
    })
    public ResponseEntity<FileSystemResource> getContent(@PathVariable Id id, @RequestHeader(value = HttpHeaders.ACCEPT, required = false) MediaType requestedMediaType) {
        throw new NoContentException(id.toString()));
    }

I would expect that the response code will be no content no matter what but while executing

curl 'http://localhost:8080/rest/1/content' -i -X GET -H 'Accept: audio/wav'

I am getting 406 (Not Acceptable) because the BasicErrorController that always attaches the body no matter what the status code is:

@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
	Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
	HttpStatus status = getStatus(request);
	return new ResponseEntity<>(body, status);
}

will later cause 406 as the body cannot be converted to the requested type. It seems to be a bug to me as according to the spec the no content response should never contain body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions