无法在浏览器或终端中显示流数据

时间:2021-06-13 23:57:21

标签: java spring-boot api stream

我正在尝试在浏览器中显示我的 Kinesis 流,但不幸的是我似乎无法做到这一点。然而,包含数据的 while 循环确实在我的 intellij 控制台中正确打印。

我可以将其包装起来并在 return 语句中调用流吗?

服务-

 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);
}

控制器 -

@RequestMapping(value = "/request-location", method= {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity requestLocation(@RequestParam MultiValueMap<String,String> form) {

    streamService.addDataToStream(form);
    streamService.buildShardIterator();
    streamService.getLocationData();
    return streamService.getLocationData();
}

while 循环的控制台输出,我希望在浏览器中看到的内容。

Intellij console output

0 个答案:

没有答案