无法提前使用next()

时间:2019-01-28 22:03:42

标签: java angular spring typescript spring-rest

我有一个要在Spring rest API中返回的对象列表,然后将其作为Angular中的对象数组读取:

public Stream<PaymentTransactions> findListByReference_transaction_id(Integer id);

我尝试过:

@GetMapping("/reference_transaction_id/{id}")
public List<ResponseEntity<PaymentTransactionsDTO>> getByListReference_transaction_id(@PathVariable String id) {
    return transactionService
            .findListByReference_transaction_id(Integer.parseInt(id))
            .map(mapper::toDTO)
            .map(ResponseEntity::ok).collect(Collectors.toList());
}

但是当我尝试将其读取为角数组时,会得到could not advance using next()从其余端点返回列表的正确方法是什么?

编辑:

@GetMapping("{id}")
    public ResponseEntity<List<ResponseEntity<PaymentTransactionsDTO>>> get(@PathVariable String id) {
        return ResponseEntity.ok(transactionService
                .findListById(Integer.parseInt(id)).stream()
                .map(mapper::toDTO)
                .map(ResponseEntity::ok).collect(Collectors.toList()));

1 个答案:

答案 0 :(得分:1)

修改您的示例:

@GetMapping("/reference_transaction_id/{id}")
@ResponseBody
public ResponseEntity<List<PaymentTransactionsDTO>> getByListReference_transaction_id(@PathVariable Integer id) {
    try(var stream = transactionService
            .findListByReference_transaction_id(id)){
      var list = stream.map(mapper::toDTO).collect(Collectors.toList());
      return list.isEmpty() ? ResponseEntity.notFound().build() : ResponseEntity.ok(list) 
    }
}
  1. 将ResponseBody添加到您的方法中
  2. 使用try-with-resource关闭流(我认为您必须将其关闭)
  3. 希望它对您有用

关于角度问题。如果您发布了一些源代码,这将有所帮助:)