Spring REST在响应中返回byte []之类的字段

时间:2017-11-16 08:29:47

标签: arrays json spring spring-restcontroller

我有一个班级ProductItem

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

和Spring休息方法:

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

如何在此课程中正确返回byte[]

1 个答案:

答案 0 :(得分:1)

您可以base64 encode将字节[]添加到ProductItem&#39; s字段中的字符串

或者

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