我有一个要在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()));
答案 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)
}
}
关于角度问题。如果您发布了一些源代码,这将有所帮助:)