如何在Spring Boot控制器中返回图像并像文件系统一样提供服务 [英] How to return an image in Spring Boot controller and serve like a file system
问题描述
我尝试过在Stackoverflow中给出的各种方法,也许我错过了一些东西。
I've tried the various ways given in Stackoverflow, maybe I missed something.
我有一个Android客户端(其代码我无法更改),这是目前得到这样的图像:
I have an Android client (whose code I can't change) which is currently getting an image like this:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
其中 url
是图像的位置(CDN上的静态资源)。现在我的Spring Boot API端点需要以相同的方式表现得像文件资源,以便相同的代码可以从API获取图像(Spring引导版本1.3.3)。
Where url
is the location of the image (static resource on CDN). Now my Spring Boot API endpoint needs to behave like a file resource in the same way so that the same code can get images from the API (Spring boot version 1.3.3).
所以我有这个:
@ResponseBody
@RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable("id")String id) {
byte[] image = imageService.getImage(id); //this just gets the data from a database
return ResponseEntity.ok(image);
}
现在Android代码试图获得 http: //someurl/image1.jpg
我的日志中出现此错误:
Now when the Android code tries to get http://someurl/image1.jpg
I get this error in my logs:
解决处理程序中的异常[public
org.springframework.http.ResponseEntity
com.myproject.MyController.getImage(java.lang.String)]:
org.springframework.web.HttpMediaTypeNotAcceptableException:
找不到可接受的表示
Resolving exception from handler [public org.springframework.http.ResponseEntity com.myproject.MyController.getImage(java.lang.String)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
当我插入 http://someurl/image1.jpg $ c时会发生同样的错误$ c>进入浏览器。
奇怪的是,我的测试检查确定:
Oddly enough my tests check out ok:
Response response = given()
.pathParam("id", "image1.jpg")
.when()
.get("MyController/Image/{id}");
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
byte[] array = response.asByteArray(); //byte array is identical to test image
如何使其表现得像服务的图像以正常方式? (注意我无法更改android代码发送的内容类型标题)
How do I get this to behave like an image being served up in the normal way? (Note I can't change the content-type header that the android code is sending)
编辑
评论后的代码(设置内容类型,取出生成
):
Code after comments (set content type, take out produces
):
@RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable("id")String id, HttpServletResponse response) {
byte[] image = imageService.getImage(id); //this just gets the data from a database
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
return ResponseEntity.ok(image);
}
在浏览器中,这似乎只是一个字符串化的垃圾(字节到字符我猜测)。在Android中它没有错误,但图像没有显示。
In a browser this just seems to give a stringified junk (byte to chars i guess). In Android it doesn't error, but the image doesn't show.
推荐答案
最后解决了这个...我不得不将 ByteArrayHttpMessageConverter
添加到我的 WebMvcConfigurerAdapter
子类:
Finally fixed this... I had to add a ByteArrayHttpMessageConverter
to my WebMvcConfigurerAdapter
subclass:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
final List<MediaType> list = new ArrayList<>();
list.add(MediaType.IMAGE_JPEG);
list.add(MediaType.APPLICATION_OCTET_STREAM);
arrayHttpMessageConverter.setSupportedMediaTypes(list);
converters.add(arrayHttpMessageConverter);
super.configureMessageConverters(converters);
}
这篇关于如何在Spring Boot控制器中返回图像并像文件系统一样提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!