java注释属性GetMapping的值。products必须是一个常量表达式
我想实现图像下载的端点。我试过这个:
@GetMapping(value = "/files/{merchant_id}", produces = { MediaType.IMAGE_PNG })
public void getFile(@PathVariable("merchant_id") Integer merchant_id, HttpServletResponse response) {
try {
File initialFile = new File("/opt/1/Why_Brookfield_Callout_3x.png");
InputStream is = FileUtils.openInputStream(initialFile);
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
} catch (IOException ex) {
throw new RuntimeException("IOError writing file to output stream");
}
}
但是我在Eclipse中得到了一个错误:注释属性GetMapping的值。products必须是一个常量表达式
你知道我如何解决这个问题吗
# 1 楼答案
问题出在
MediaType.IMAGE_PNG
上,它返回一个MediaType对象您应该使用的正确选项是
MediaType.IMAGE_PNG_VALUE
,这将返回一个字符串因此,您可以使用以下行进行修复:
@GetMapping(value = "/files/{merchant_id}", produces = { MediaType.IMAGE_PNG_VALUE })
编辑:我没有评论帖子的名声,但我想知道这个方法是否应该返回除void以外的内容
致以最良好的祝愿