我有一个要在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()));

最佳答案

修改了您的示例:

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



将ResponseBody添加到您的方法
使用try-with-resource关闭流(我认为您必须关闭它)
希望这对你有用


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

10-04 15:25