我正在尝试在我的 Spring WebMVC 3.0.5 Controller 中使用 ResponseEntity 返回类型。我正在返回一张图片,所以我想使用以下代码将 Content Type 设置为 image/gif:
@RequestMapping(value="/*.gif")
public ResponseEntity<Resource> sendGif() throws FileNotFoundException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_GIF);
return new ResponseEntity<Resource>(ctx.getResource("/images/space.gif"), headers, HttpStatus.OK);
}
但是,ResourceHttpMessageConverter 中的返回类型被覆盖为 text/html。
除了实现我自己的 HttpMessageConverter 并将其注入(inject) AnnotationMethodHandlerAdapter 之外,我还有什么方法可以强制 Content-Type 吗?
最佳答案
另一个命题:
return ResponseEntity
.ok()
.contentType(MediaType.IMAGE_GIF)
.body(resource);
关于spring - 使用 ResponseEntity<Resource> 发送自定义 Content-Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6182362/