春季 RestController 返回错误的内容类型

huangapple go评论77阅读模式
英文:

Spring RestController returns wrong content type

问题

我正尝试以以下方式在Spring RestController中返回图像

    @GetMapping(path = "/images/{imageKey:.+}")
    public ResponseEntity<Resource> getImageAsResource(
            @PathVariable("imageKey") String imageKey) {
        Resource resource = resourceService.getImage(imageKey);
        return ResponseEntity.ok(resource);
    }

`resourceService` 将图像作为Java Resource对象返回然而Spring将 `Content-Type:` 设置为 `application/json`,而不是根据生成的HTTP响应中的资源设置为正确的 `image/...` 类型
我如何让Spring从返回的资源中推断出正确的内容类型
返回的图像资源可以是PNGJPG或GIF格式
英文:

I am trying to return an image in a Spring RestController in the following way:

@GetMapping(path = &quot;/images/{imageKey:.+}&quot;)
public ResponseEntity&lt;Resource&gt; getImageAsResource(
        @PathVariable(&quot;imageKey&quot;) String imageKey) {
    Resource resource = resourceService.getImage(imageKey);
    return ResponseEntity.ok(resource);
}

The resourceService returns the image as a Java Resource object. Spring however sets the Content-Type: to application/json instead of the correct image/... type depending on the resource in the generated HTTP response.
How can I make Spring infer the correct content type from the returned resource?
The returned image resource may be a PNG, a JPG or a GIF.

答案1

得分: 2

@GetMapping(path = "/images/{imageKey:.+}")
public ResponseEntity<Resource> getImageAsResource(
        @PathVariable("imageKey") String imageKey) {
    Resource resource = resourceService.getImage(imageKey);
    Map<String, String> headers = new HashMap<>();
    //Change it based on the type of image your are loading
    headers.put("Content-type", MediaType.IMAGE_JPEG_VALUE);
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
英文:
    @GetMapping(path = &quot;/images/{imageKey:.+}&quot;)
	public ResponseEntity&lt;Resource&gt; getImageAsResource(
			@PathVariable(&quot;imageKey&quot;) String imageKey) {
		Resource resource = resourceService.getImage(imageKey);
		Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();
		//Change it based on the type of image your are loading
		headers.put(&quot;Content-type&quot;, MediaType.IMAGE_JPEG_VALUE);
		return new ResponseEntity&lt;&gt;(resource, headers, HttpStatus.OK);
	}

答案2

得分: 2

在映射方法中指定所生成的内容类型。如果您不需要控制HTTP头和响应代码,请使用@ResponseBody注解。

@ResponseBody
@GetMapping(path = "/images/{imageKey:.+}", produces = MediaType.IMAGE_JPEG_VALUE)
public Resource getImageAsResource(
                         @PathVariable("imageKey") String imageKey) {
    return resourceService.getImage(imageKey);
}
英文:

Specify content type produced by the method in the mapping. Use @ResponseBodyannotation if you don't need control over HTTP headers & response codes.

 @ResponseBody
 @GetMapping(path = &quot;/images/{imageKey:.+}&quot;, produces = MediaType. IMAGE_JPEG_VALUE)
 public Resource getImageAsResource(
                         @PathVariable(&quot;imageKey&quot;) String imageKey) {
    return resourceService.getImage(imageKey);
 }

huangapple
  • 本文由 发表于 2020年7月24日 21:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63074447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定