【问题标题】:Spring REST return byte[] like field in responseSpring REST 返回字节 [] 类似字段作为响应
【发布时间】:2017-11-16 08:29:46
【问题描述】:

我有一堂课ProductItem

@Data
public class ProductItem {
  private String name;
  private String description;
  private byte[] image;
}

还有Spring rest方法:

@GetMapping(value = "/test")
private ResponseEntity<ProductItem> findProduct() {
    ProductItem i= service.getProductItem()
    return ResponseEntity.ok(i);
}

我怎样才能在这个类中正确返回byte[]

【问题讨论】:

  • 你想只返回字节还是整个类?
  • 有什么问题,它应该可以工作,我认为唯一缺少的是在映射中产生。您的映射应该是 @GetMapping(value = "/test",produces = MediaType.APPLICATION_JSON_VALUE)

标签: arrays json spring spring-restcontroller


【解决方案1】:

您可以base64 encode byte[] 到您 ProductItem 字段中的字符串

或者

您可以将 HttpServletReponse 添加到您的方法和write the byte[] in the response's OutputStream。在这种情况下不需要返回 ResponseEntity。

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2022-01-23
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多