In Spring MVC, how can I set the mime type header when using @ResponseBody

I have a Spring MVC controller that returns a JSON string, and I would like to set mimetype to application / json. How can i do this?

@RequestMapping(method=RequestMethod.GET, value="foo/bar") @ResponseBody public String fooBar(){ return myService.getJson(); } 

Business objects are already available as JSON strings, so using MappingJacksonJsonView is not a solution for me. @ResponseBody is perfect, but how can I set the mimetype type?

+62
java json spring spring-mvc controller
Dec 17 2018-10-17
source share
7 answers

I would decide to reorganize the service to return a domain object, not JSON strings, and let Spring handle serialization (via MappingJacksonHttpMessageConverter when writing). Starting with Spring 3.1, the implementation looks pretty neat:

 @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET value = "/foo/bar") @ResponseBody public Bar fooBar(){ return myService.getBar(); } 

Comments:

Firstly, <mvc:annotation-driven /> or @EnableWebMvc should be added to your application configuration.

The produces attribute of @RequestMapping annotations is @RequestMapping used to indicate the type of response content. Therefore, it must be set to MediaType.APPLICATION_JSON_VALUE (or "application/json" ).

Finally, Jackson should be added so that any serialization and de-serialization between Java and JSON will be handled automatically by Spring (Jackson's dependency is determined by Spring, and MappingJacksonHttpMessageConverter will be under the hood).

+34
Nov 24 '12 at 16:28
source share
— -

Use ResponseEntity instead of ResponseBody . This way you have access to the response headers, and you can set the appropriate type of content. According to Spring docs :

HttpEntity is similar to @RequestBody and @ResponseBody . In addition to accessing the request and the response body, HttpEntity (and the response-specific subclass of ResponseEntity ) also allows access to request and response headers

The code will look like this:

 @RequestMapping(method=RequestMethod.GET, value="/fooBar") public ResponseEntity<String> fooBar2() { String json = "jsonResponse"; HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); return new ResponseEntity<String>(json, responseHeaders, HttpStatus.CREATED); } 
+104
Dec 19 '10 at 14:34
source share

You may not be able to do this with @ResponseBody, but something like this should work:

 package xxx; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class FooBar { @RequestMapping(value="foo/bar", method = RequestMethod.GET) public void fooBar(HttpServletResponse response) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(myService.getJson().getBytes()); response.setContentType("application/json"); response.setContentLength(out.size()); response.getOutputStream().write(out.toByteArray()); response.getOutputStream().flush(); } } 
+6
Dec 17 '10 at 15:26
source share

I do not think that's possible. It seems to him open Jira:

SPR-6702: explicitly set Content-Type response in @ResponseBody

+4
Dec 17 2018-10-17
source share

Register org.springframework.http.converter.json.MappingJacksonHttpMessageConverter as a message converter and return the object directly from the method.

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"/> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean> 

and controller:

 @RequestMapping(method=RequestMethod.GET, value="foo/bar") public @ResponseBody Object fooBar(){ return myService.getActualObject(); } 

This requires an org.springframework:spring-webmvc .

+3
Dec 17 2018-10-17
source share

I do not think you can except response.setContentType(..)

+1
Dec 17 2018-10-17
source share

My version of reality. Download HTML file and stream to browser.

 @Controller @RequestMapping("/") public class UIController { @RequestMapping(value="index", method=RequestMethod.GET, produces = "text/html") public @ResponseBody String GetBootupFile() throws IOException { Resource resource = new ClassPathResource("MainPage.html"); String fileContents = FileUtils.readFileToString(resource.getFile()); return fileContents; } } 
-one
Sep 29 '16 at 22:17
source share



All Articles