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:
parent
f7943786de
commit
dc01f088f7
|
@ -196,7 +196,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;
|
||||
}
|
||||
|
||||
}
|
|
@ -301,35 +301,6 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||
verify(messageConverter);
|
||||
}
|
||||
|
||||
// SPR-9160
|
||||
|
||||
@Test
|
||||
public void handleReturnValueSortByQuality() throws Exception {
|
||||
this.servletRequest.addHeader("Accept", "text/plain; q=0.5, application/json");
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new MappingJackson2HttpMessageConverter());
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
RequestResponseBodyMethodProcessor handler = new RequestResponseBodyMethodProcessor(converters);
|
||||
|
||||
handler.writeWithMessageConverters("Foo", returnTypeStringProduces, webRequest);
|
||||
|
||||
assertEquals("application/json;charset=UTF-8", servletResponse.getHeader("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnValueString() throws Exception {
|
||||
List<HttpMessageConverter<?>>converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new ByteArrayHttpMessageConverter());
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
|
||||
processor = new RequestResponseBodyMethodProcessor(converters);
|
||||
processor.handleReturnValue("Foo", returnTypeString, mavContainer, webRequest);
|
||||
|
||||
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
|
||||
assertEquals("Foo", servletResponse.getContentAsString());
|
||||
}
|
||||
|
||||
|
||||
@ResponseBody
|
||||
public String handle1(@RequestBody String s, int i) {
|
||||
|
|
|
@ -143,12 +143,25 @@ 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"));
|
||||
}
|
||||
|
||||
|
||||
public String handle(@RequestBody List<SimpleBean> list, @RequestBody SimpleBean simpleBean) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static class SimpleBean {
|
||||
|
||||
private String name;
|
||||
|
|
Loading…
Reference in New Issue