简体   繁体   中英

Spring Return error format in ResponseEntity

I try to return the byte[] object by using ResponseEntity

return new ResponseEntity<>(new Response(ResultCode.SUCCESS, SUCCESS).setResult(dataObj), HttpStatus.OK);

but and got a response like that: 在此处输入图片说明

and another way I try to return the direct byte[] object

return dataObj;

the response like that: 在此处输入图片说明

Why do we have different? and Can I return the ResponseEntity with data like the 2nd image?

Try following code. It will send image itself in response on accessing this API.

@GetMapping(value = "/image/{id}")
public ResponseEntity<?> getImageById(@PathParam Long id) throws Exception {
  String imageData = this.entityRepository.findById(id).orElse(null).getData();
  byte[] imageByte = Base64.getDecoder().decode(new String(imageData).getBytes("UTF-8"));
  MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(mediaType);
  return new ResponseEntity<>(imageByte, headers, HttpStatus.OK);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM