-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Closed
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)type: bugA general bugA general bug
Milestone
Description
David Melia opened SPR-6970 and commented
Hi,
the method protected boolean canWrite(MediaType mediaType) in org.springframework.http.converter.AbstractHttpMessageConverter is incorrect.
/**
* Returns true if the given media type includes any of the
* {@linkplain #setSupportedMediaTypes(List) supported media types}.
* @param mediaType the media type
* @return true if the supported media types include the media type, or if the media type is {@code null}
*/
protected boolean canWrite(MediaType mediaType) {
if (mediaType == null) {
return true;
}
for (MediaType supportedMediaType : getSupportedMediaTypes()) {
if (mediaType.includes(supportedMediaType)) {
return true;
}
}
return false;
}
I think the line
if (mediaType.includes(supportedMediaType)
should be
if (supportedMediaType.includes(mediaType)
The reason I think this is:
- the javadoc states "
@returntrue if the supported media types include the media type ..." - I am using the MarshallingHttpMessageConverter which supports application/+xml by default. In my HTTP GET I am passing in accept="application/whatever+xml". so in the above canWrite tries to check if "application/whatever+xml" includes "application/+xml" which doesn't really make much sense (and also returns false obviously).
Check out my main test below which always returns false
public static void main(String[] args) {
MediaType soapMediaType = new MediaType("application","soap+xml");
MarshallingHttpMessageConverter msgConverter = new MarshallingHttpMessageConverter() {
@Override
public boolean supports(Class<?> clazz) {
return true;
}
};
System.out.println(msgConverter.canWrite(Object.class, soapMediaType)); // prints out false but I think should be true.
}
Thanks
Affects: 3.0.1
Issue Links:
- Using @ResponseBody throws HttpMediaTypeNotAcceptableException when it supports writing an acceptable MediaType [SPR-6214] #10882 Using
@ResponseBodythrows HttpMediaTypeNotAcceptableException when it supports writing an acceptable MediaType - @ResponseBody throws HttpMediaTypeNotAcceptableException if client accepts "*/*" [SPR-6686] #11352
@ResponseBodythrows HttpMediaTypeNotAcceptableException if client accepts "/"
Referenced from: commits 85b8bef
Metadata
Metadata
Assignees
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)type: bugA general bugA general bug