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()));
                I’m just guessing: The spring-data method returns a Stream. You have probably already closed the connection before returning back to the controller, so the Stream is unusable at this point. Just use List as the return value for the repository. Also, the controller should return ResponseEntity<List<...>>, not List<ResponseEntity<...>>. Also, this question doesn’t have anything to do with Angular. Also, post full stack trace.
– Mateusz Stefek
                Jan 28, 2019 at 22:41
                Thanks but how to return ResponseEntity<List<...>>? Can you show me how to modify the code?
– Peter Penzov
                Jan 28, 2019 at 22:49
                Change the order of wrapping: return ResponseEntity.ok(...thewholeexpressionthatgeneratesalist...)
– Mateusz Stefek
                Jan 28, 2019 at 22:52
@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 :)

    Should I add ResponseEntity.ok(...) and .map(ResponseEntity::ok).orElseGet(() -> notFound().build());? – Peter Penzov Jan 28, 2019 at 23:11

    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.