java在流式响应/api调用中未接收数据
我试图在我的控制器中流式传输特定方法的响应,但它没有给我任何数据。有没有更好的方法来实现这一点
方法是streamService。getLocationData()
预期产量-
{uncertaintyRadiusInMeters=[64], latitude=[12.345678], longitude=[-12.345678], dateTimeCaptured=[1623431888949]}
SpringBoot控制器-
@RequestMapping(value = "/request-location", method= {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<StreamingResponseBody> requestLocation(@RequestParam MultiValueMap<String,String> form) {
streamService.addDataToStream(form);
streamService.buildShardIterator();
StreamingResponseBody responseBody = outputStream -> streamService.getLocationData();
return ResponseEntity.ok()
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(responseBody);
//返回streamService。getLocationData(); }
服务-
public ResponseEntity getLocationData() {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
AmazonKinesis kinesisClient = AmazonKinesisClient.builder()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withRegion(awsRegion)
.build();
GetRecordsRequest recordsRequest = new GetRecordsRequest();
recordsRequest.setShardIterator(shardIterator.getShardIterator());
recordsRequest.setLimit(1000);
GetRecordsResult recordsResult = kinesisClient.getRecords(recordsRequest);
while (!recordsResult.getRecords().isEmpty()) {
recordsResult.getRecords().stream()
.map(record -> new String(record.getData().array()))
.forEach(System.out::println);
recordsRequest.setShardIterator(recordsResult.getNextShardIterator());
kinesisClient.getRecords(recordsRequest);
try {
Thread.sleep(1000);
}
catch (InterruptedException exception) {
throw new RuntimeException(exception);
}
}
return ResponseEntity.ok(recordsResult);
}
共 (0) 个答案