在Spring MVC中,如何在使用@ResponseBody时设置MIMEtypes标题

我有一个Spring MVC控制器返回一个JSONstring,我想将mimetype设置为application / json。 我怎样才能做到这一点?

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

业务对象已经可以作为JSONstring,所以使用MappingJacksonJsonView不是我的解决scheme。 @ResponseBody是完美的,但我怎样才能设置MIMEtypes?

我会考虑重构服务来返回你的域对象而不是JSONstring,并让Spring处理序列化(通过MappingJacksonHttpMessageConverter )。 从Spring 3.1开始,实现看起来非常整齐:

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

注释:

首先,必须将<mvc:annotation-driven />@EnableWebMvc 添加到您的应用程序configuration中。

接下来,@ @RequestMapping注释的@RequestMapping属性用于指定响应的内容types。 因此,应将其设置为MediaType.APPLICATION_JSON_VALUE (或"application/json" )。

最后,必须添加Jackson,以便Java和JSON之间的任何序列化和反序列化将由Spring自动处理(Jackson的依赖被Spring检测到, MappingJacksonHttpMessageConverter将被MappingJacksonHttpMessageConverter )。

使用ResponseEntity而不是ResponseBody 。 这样你就可以访问响应头,你可以设置适当的内容types。 根据春季文件 :

HttpEntity类似于@RequestBody@ResponseBody 。 除了访问请求和响应主体之外, HttpEntity (和特定于响应的子类ResponseEntity )还允许访问请求和响应头

代码将如下所示:

 @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); } 

您可能无法使用@ResponseBody来做到这一点,但是应该这样做:

 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(); } } 

我不认为这是可能的。 似乎有一个开放的吉拉:

SPR-6702:在@ResponseBody中显式设置响应Content-Type

注册org.springframework.http.converter.json.MappingJacksonHttpMessageConverter作为消息转换器,并直接从方法返回对象。

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

和控制器:

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

这需要依赖org.springframework:spring-webmvc

我不认为你可以,除了response.setContentType(..)

我的版本的现实。 加载一个HTML文件并stream式传输到浏览器。

 @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; } }