Spring REST如何响应带有二进制字段的对象



我如何发送响应与Spring MVC这个对象:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DeviceSummaryResponse {
@ApiModelProperty(value = "Конфигурация устройства", required = true)
private ConfigResponse config;
@ApiModelProperty(value = "GPX файл с маршрутом устройства", required = true)
private InputStream route;
}

我尝试这个,但它不工作,由于'route'字段,转换器错误。

@GetMapping(DEVICE_ID_PATH)
public ResponseEntity<DeviceSummaryResponse> getDeviceSummary(@PathVariable(name = "id") Long id) {
log.debug("REST request to get summary for complex with id = {}", id);
var deviceSummary = mobileComplexService.getSummary(id);
return ResponseEntity.ok().body(deviceSummaryMapper.toResponse(deviceSummary));
}

我需要按照规范中的描述做:

DeviceSummaryResponse:
type: object
properties:
config:
type: object
properties:
...
...
route:
type: string
format: binary
description: GPX файл с маршрутом устройства
required:
- config
- route

提前谢谢你

您可以在InputStream中使用readAllBytes(),并使用String的字节数组构造函数。

InputStream route;
…
byte[] routeBytes = route.readAllBytes();
String routeBinary = new String(routeBytes, StandardCharsets.UTF_8); // for UTF-8 encoding

最新更新