提交 8d5fc2bf 编写于 作者: A Arjen Poutsma

SPR-6686 - @ResponseBody throws HttpMediaTypeNotAcceptableException if client accepts "*/*"

上级 f7952fcc
......@@ -864,6 +864,9 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
if (acceptedMediaTypes.isEmpty()) {
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
}
else {
Collections.sort(acceptedMediaTypes);
}
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
Class<?> returnValueType = returnValue.getClass();
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
......@@ -872,7 +875,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
for (MediaType acceptedMediaType : acceptedMediaTypes) {
if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
messageConverter.write(returnValue, null, outputMessage);
messageConverter.write(returnValue, acceptedMediaType, outputMessage);
this.responseArgumentUsed = true;
return;
}
......
......@@ -92,7 +92,26 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* type.
*/
public boolean canRead(Class<? extends T> clazz, MediaType mediaType) {
return supports(clazz) && isSupported(mediaType);
return supports(clazz) && canRead(mediaType);
}
/**
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types} include the given media
* type.
*
* @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 canRead(MediaType mediaType) {
if (mediaType == null) {
return true;
}
for (MediaType supportedMediaType : getSupportedMediaTypes()) {
if (supportedMediaType.includes(mediaType)) {
return true;
}
}
return false;
}
/**
......@@ -103,22 +122,22 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* type.
*/
public boolean canWrite(Class<? extends T> clazz, MediaType mediaType) {
return supports(clazz) && isSupported(mediaType);
return supports(clazz) && canWrite(mediaType);
}
/**
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types} include the given media
* type.
* 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 isSupported(MediaType mediaType) {
protected boolean canWrite(MediaType mediaType) {
if (mediaType == null) {
return true;
}
for (MediaType supportedMediaType : getSupportedMediaTypes()) {
if (supportedMediaType.includes(mediaType)) {
if (mediaType.includes(supportedMediaType)) {
return true;
}
}
......@@ -165,7 +184,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
public final void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
HttpHeaders headers = outputMessage.getHeaders();
if (contentType == null) {
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentType = getDefaultContentType(t);
}
if (contentType != null) {
......
......@@ -89,12 +89,12 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
JavaType javaType = TypeFactory.fromClass(clazz);
return objectMapper.canDeserialize(javaType) && isSupported(mediaType);
return objectMapper.canDeserialize(javaType) && canRead(mediaType);
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return objectMapper.canSerialize(clazz) && isSupported(mediaType);
return objectMapper.canSerialize(clazz) && canWrite(mediaType);
}
@Override
......
......@@ -52,12 +52,12 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return (clazz.isAnnotationPresent(XmlRootElement.class) || clazz.isAnnotationPresent(XmlType.class)) &&
isSupported(mediaType);
canRead(mediaType);
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && isSupported(mediaType);
return AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType);
}
@Override
......
......@@ -36,6 +36,17 @@ public class ByteArrayHttpMessageConverterTests {
converter = new ByteArrayHttpMessageConverter();
}
@Test
public void canRead() {
assertTrue(converter.canRead(byte[].class, new MediaType("application", "octet-stream")));
}
@Test
public void canWrite() {
assertTrue(converter.canWrite(byte[].class, new MediaType("application", "octet-stream")));
assertTrue(converter.canWrite(byte[].class, MediaType.ALL));
}
@Test
public void read() throws IOException {
byte[] body = new byte[]{0x1, 0x2};
......
......@@ -40,6 +40,19 @@ public class FormHttpMessageConverterTests {
converter = new FormHttpMessageConverter();
}
@Test
@SuppressWarnings("unchecked")
public void canRead() {
assertTrue(converter.canRead((Class<? extends MultiValueMap<String, String>>) MultiValueMap.class, new MediaType("application", "x-www-form-urlencoded")));
}
@Test
@SuppressWarnings("unchecked")
public void canWrite() {
assertTrue(converter.canWrite((Class<? extends MultiValueMap<String, String>>) MultiValueMap.class, new MediaType("application", "x-www-form-urlencoded")));
assertTrue(converter.canWrite((Class<? extends MultiValueMap<String, String>>) MultiValueMap.class, MediaType.ALL));
}
@SuppressWarnings("unchecked")
@Test
public void read() throws Exception {
......
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
/**
* Test-case for AbstractHttpMessageConverter.
*
* @author Arjen Poutsma
*/
public class HttpMessageConverterTests {
private static final MediaType MEDIA_TYPE = new MediaType("foo", "bar");
@Test
public void canRead() {
AbstractHttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(MEDIA_TYPE) {
@Override
protected boolean supports(Class<? extends MyType> clazz) {
return MyType.class.equals(clazz);
}
};
assertTrue(converter.canRead(MyType.class, MEDIA_TYPE));
assertFalse(converter.canRead(MyType.class, new MediaType("foo", "*")));
assertFalse(converter.canRead(MyType.class, MediaType.ALL));
}
@Test
public void canWrite() {
AbstractHttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(MEDIA_TYPE) {
@Override
protected boolean supports(Class<? extends MyType> clazz) {
return MyType.class.equals(clazz);
}
};
assertTrue(converter.canWrite(MyType.class, MEDIA_TYPE));
assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "*")));
assertTrue(converter.canWrite(MyType.class, MediaType.ALL));
}
private static class MyHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> {
private MyHttpMessageConverter(MediaType supportedMediaType) {
super(supportedMediaType);
}
@Override
protected boolean supports(Class<? extends T> clazz) {
fail("Not expected");
return false;
}
@Override
protected T readInternal(Class<T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
fail("Not expected");
return null;
}
@Override
protected void writeInternal(T t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
fail("Not expected");
}
}
private static class MyType {
}
}
......@@ -37,6 +37,17 @@ public class StringHttpMessageConverterTests {
converter = new StringHttpMessageConverter();
}
@Test
public void canRead() {
assertTrue(converter.canRead(String.class, new MediaType("text", "plain")));
}
@Test
public void canWrite() {
assertTrue(converter.canWrite(String.class, new MediaType("text", "plain")));
assertTrue(converter.canWrite(String.class, MediaType.ALL));
}
@Test
public void read() throws IOException {
String body = "Hello World";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册