Try defaulContentType for application/octet-stream
AbstractHttpMessageConverter now tries to call getDefaultContentType with the actual value to be converted to see if that will result in a more concrete mediat type than application/octet-stream. Issue: SPR-12894
This commit is contained in:
parent
b0848db17c
commit
8ff7cc73bc
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -174,6 +174,10 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
|
||||||
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
|
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
|
||||||
contentTypeToUse = getDefaultContentType(t);
|
contentTypeToUse = getDefaultContentType(t);
|
||||||
}
|
}
|
||||||
|
else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
|
||||||
|
MediaType type = getDefaultContentType(t);
|
||||||
|
contentTypeToUse = (type != null ? type : contentTypeToUse);
|
||||||
|
}
|
||||||
if (contentTypeToUse != null) {
|
if (contentTypeToUse != null) {
|
||||||
headers.setContentType(contentTypeToUse);
|
headers.setContentType(contentTypeToUse);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,9 @@ import org.junit.Test;
|
||||||
import org.springframework.aop.framework.ProxyFactory;
|
import org.springframework.aop.framework.ProxyFactory;
|
||||||
import org.springframework.aop.target.SingletonTargetSource;
|
import org.springframework.aop.target.SingletonTargetSource;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpInputMessage;
|
import org.springframework.http.HttpInputMessage;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
@ -38,6 +41,7 @@ import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
import org.springframework.http.converter.ResourceHttpMessageConverter;
|
||||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
||||||
|
@ -49,6 +53,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||||
|
@ -285,6 +290,24 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||||
assertEquals("text/plain;charset=UTF-8", servletResponse.getHeader("Content-Type"));
|
assertEquals("text/plain;charset=UTF-8", servletResponse.getHeader("Content-Type"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPR-12894
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleReturnValueImage() throws Exception {
|
||||||
|
this.servletRequest.addHeader("Accept", "*/*");
|
||||||
|
|
||||||
|
Method method = getClass().getMethod("getImage");
|
||||||
|
MethodParameter returnType = new MethodParameter(method, -1);
|
||||||
|
|
||||||
|
List<HttpMessageConverter<?>> converters = Arrays.asList(new ResourceHttpMessageConverter());
|
||||||
|
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
|
||||||
|
|
||||||
|
ClassPathResource resource = new ClassPathResource("logo.jpg", getClass());
|
||||||
|
processor.writeWithMessageConverters(resource, returnType, this.webRequest);
|
||||||
|
|
||||||
|
assertEquals("image/jpeg", this.servletResponse.getHeader("Content-Type"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void supportsReturnTypeResponseBodyOnType() throws Exception {
|
public void supportsReturnTypeResponseBodyOnType() throws Exception {
|
||||||
Method method = ResponseBodyController.class.getMethod("handle");
|
Method method = ResponseBodyController.class.getMethod("handle");
|
||||||
|
@ -512,6 +535,10 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public Resource getImage() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static abstract class MyParameterizedController<DTO extends Identifiable> {
|
private static abstract class MyParameterizedController<DTO extends Identifiable> {
|
||||||
|
|
Loading…
Reference in New Issue