diff --git a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java index 25d5faf5c1..1190341e9d 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java @@ -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"); * you may not use this file except in compliance with the License. @@ -174,6 +174,10 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentTypeToUse = getDefaultContentType(t); } + else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) { + MediaType type = getDefaultContentType(t); + contentTypeToUse = (type != null ? type : contentTypeToUse); + } if (contentTypeToUse != null) { headers.setContentType(contentTypeToUse); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java index be6a90944a..7bf0220c89 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java @@ -30,6 +30,9 @@ import org.junit.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.target.SingletonTargetSource; 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.HttpInputMessage; 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.HttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.ResourceHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 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.annotation.RequestBody; 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.RestController; import org.springframework.web.bind.support.WebDataBinderFactory; @@ -285,6 +290,24 @@ public class RequestResponseBodyMethodProcessorTests { 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> 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 public void supportsReturnTypeResponseBodyOnType() throws Exception { Method method = ResponseBodyController.class.getMethod("handle"); @@ -512,6 +535,10 @@ public class RequestResponseBodyMethodProcessorTests { return null; } + @SuppressWarnings("unused") + public Resource getImage() { + return null; + } @SuppressWarnings("unused") private static abstract class MyParameterizedController {