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

This commit is contained in:
Arjen Poutsma
2010-01-15 10:23:59 +00:00
parent f7952fccc8
commit 8d5fc2bf91
8 changed files with 167 additions and 12 deletions

View File

@@ -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) {

View File

@@ -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

View File

@@ -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