I'm trying to return an image on spring-boot (1.2.2)
How should I set the content-type?
Non of the following are working for me (meaning that response headers are not containing 'content-type' header at all ):
@RequestMapping(value = "/files2/{file_name:.+}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getFile2(final HttpServletResponse response) throws IOException {
InputStream is = //someInputStream...
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.setContentType("image/jpeg");
InputStreamResource inputStreamR = new InputStreamResource(is);
return new ResponseEntity<>(inputStreamR, HttpStatus.OK);
}
@RequestMapping(value = "/files3/{file_name:.+}", method = RequestMethod.GET)
public HttpEntity<byte[]> getFile3() throws IOException {
InputStream is = //someInputStream...
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new HttpEntity<>(IOUtils.toByteArray(is), headers);
}
This article explains how to set content type in Spring MVC, first adding Json mapper in the classpath, then using ResponseEntity, and finally changing the return type from String to Map. As always, all the code snippets can be found over on GitHub.
The endpoint accepts a JSON body to avoid a list of parameters (i.e. query parameters). When I try to build the test in Spring I get this error: This example code should give you an idea of the original issue: The exchange method throws an error and return 400 BAD_REQUEST. The GET Spring Boot mapping is nothing complicated:
Content-Type Not Being Set Properly When a method has a return type String, and no JSON Mapper present at classpath. In this case, the return value is handled by StringHttpMessageConverter class which sets the content type to “text/plain”.
If Jackson is on the classpath, any controller in Spring applications renders the JSON response by default. To include Jackson on the classpath, we need to add the following dependency in pom.xml:
Firstly, you'll need to apply the @ResponseBody
annotation in addition to @RequestMapping
, unless you are using @RestController
at the class level instead of just @Controller
. Also, try the produces
element of @RequestMapping
e.g.
@RequestMapping(value = "/files2/{file_name:.+}", method = RequestMethod.GET, produces = {MediaType.IMAGE_JPEG_VALUE})
This should 'narrow the primary mapping' and ensure the correct content type is set. See the docs: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-requestmapping-produces
Got it... Had to add ByteArrayHttpMessageConverter
to WebConfiguration
class:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> httpMessageConverters) {
httpMessageConverters.add(new ByteArrayHttpMessageConverter());
}
}
And the then my second attempt (getFile3()
) was working correctly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With