Java 响应中的弹簧复位错误格式
我尝试使用ResponseEntity返回byte[]对象Java 响应中的弹簧复位错误格式,java,spring,spring-boot,Java,Spring,Spring Boot,我尝试使用ResponseEntity返回byte[]对象 return new ResponseEntity<>(new Response(ResultCode.SUCCESS, SUCCESS).setResult(dataObj), HttpStatus.OK); 回答是这样的: 为什么我们有不同的选择?我能用第二张图像这样的数据返回响应吗?试试下面的代码。它将在访问此API时发送映像本身作为响应 @GetMapping(value = "/image/{id}&
return new ResponseEntity<>(new Response(ResultCode.SUCCESS, SUCCESS).setResult(dataObj), HttpStatus.OK);
回答是这样的:
为什么我们有不同的选择?我能用第二张图像这样的数据返回响应吗?试试下面的代码。它将在访问此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);
}
@GetMapping(value=“/image/{id}”)
公共响应性getImageById(@PathParam Long id)引发异常{
String imageData=this.entityRepository.findById(id).orElse(null).getData();
byte[]imageByte=Base64.getDecoder().decode(新字符串(imageData.getBytes(“UTF-8”));
MediaType MediaType=MediaType.APPLICATION\u OCTET\u STREAM;
HttpHeaders=新的HttpHeaders();
headers.setContentType(mediaType);
返回新的ResponseEntity(imageByte、headers、HttpStatus.OK);
}
@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);
}