Fix issue in message conversion

This change ensures that when the Accept and the Producible media types
are equally specific, we use the one from the Accept header, which may
for example carry a different charset.
This commit is contained in:
Rossen Stoyanchev
2012-10-12 09:20:55 -04:00
parent 5e318e73f8
commit 7c1a2f37f2
2 changed files with 16 additions and 1 deletions

View File

@@ -193,7 +193,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
*/
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
produceType = produceType.copyQualityValue(acceptType);
return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) < 0 ? acceptType : produceType;
return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) <= 0 ? acceptType : produceType;
}
}

View File

@@ -315,6 +315,21 @@ public class RequestResponseBodyMethodProcessorTests {
assertEquals("Foo", servletResponse.getContentAsString());
}
@Test
public void handleReturnValueStringAcceptCharset() throws Exception {
this.servletRequest.addHeader("Accept", "text/plain;charset=UTF-8");
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
processor.writeWithMessageConverters("Foo", returnTypeString, webRequest);
assertEquals("text/plain;charset=UTF-8", servletResponse.getHeader("Content-Type"));
}
@ResponseBody
public String handle1(@RequestBody String s, int i) {
return s;