Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot setContentType is not working

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);
}
like image 925
Shoham Avatar asked Jun 08 '15 12:06

Shoham


People also ask

How do I set content type in Spring MVC?

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.

Why does the Spring Boot endpoint accept a JSON body?

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:

Why is my content type not set to plain text?

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”.

How to include Jackson on the classpath in Spring Boot?

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:


2 Answers

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

like image 95
Tom Bunting Avatar answered Sep 29 '22 11:09

Tom Bunting


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

like image 38
Shoham Avatar answered Sep 29 '22 11:09

Shoham