1

I have a class ProductItem

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

And Spring rest method:

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

How can I correctly return byte[] in this class?

2
  • do you want to return the byte only or the whole class? Commented Nov 16, 2017 at 8:45
  • what is issue, it should work, only thing i think is missing is produces in mapping. Your mapping should be @GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE) Commented Nov 16, 2017 at 9:52

1 Answer 1

3

You can base64 encode the byte[] to a String in you ProductItem's field

Or

You can add HttpServletReponse to your method and write the byte[] in the response's OutputStream. In this case no need to return ResponseEntity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.