Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a list of objects which I would like to return in Spring rest API and then read it as an array of objects in Angular:
public Stream<PaymentTransactions> findListByReference_transaction_id(Integer id);
I tried this:
@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());
But when I try to read it as an Angular Array I get could not advance using next()
What is the proper way to return a List from the rest endpoint?
Edit:
@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)
Add ResponseBody to your method
Close the stream with a try-with-resource (i think you have to close it)
Hope it works for you
Regarding your angular problem. It would help if you posted some source code :)
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.