Skip to content

AbstractMessageConverterMethodProcessor results in 406 even when ResponseEntity presets content type #23205

@xak2000

Description

@xak2000

The fixes introduced in #20720 and #20798 added ability to return ResponseEntity with defined ContentType header. Like this:

    @GetMapping("/test")
    public ResponseEntity<MyModel> test() {
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body(new MyModel());
    }

AbstractMessageConverterMethodProcessor now takes into account provided Content-Type header and uses it instead of Accept header from request. So it just ignores Accept header. It is ok and good.

But in this case, when no HttpMessageConverter found that can write the response with selected Content-Type, then AbstractMessageConverterMethodProcessor throws HttpMediaTypeNotAcceptableException.

		if (body != null) {
			throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
		}

I think this is not right, because it leads to 406 response, like if no acceptable representation was found, while the real reason of this error is that no converter found for selected representation (and Accept header was ignored altogether). So, this clearly indicates a server misconfiguration (server tries to return a response using provided content type without registering proper HttpMessageConverter, that supports this content type with this response body type). So it should lead to error 500 and should throw HttpMessageNotWritableException.

It should do this only when selectedMediaType is selected based on outputMessage.getHeaders().getContentType() (the Accept header was not taken into account), of course. In other cases HttpMediaTypeNotAcceptableException is still good.

Affected version:

Spring 5.1.8.RELEASE

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions