Get all data from an external and paginated Swagger API using Java / Feign / Spring I have written the following method that allows me to import from another API all the items stored there using Feign.
The only issue is that the external API provides a size limit of 2000 and therefore I need to take pagination into account to get all the records.
I think that my solution is not very clean and maintainable and I'd like to use streams and newer Java features to make it shorter (like I did in the final part) but I am still learning and I cannot think of good solutions for the main part of the code.
@Service
public class ClientService {

    @Autowired
    private Client Client;

    @Autowired
    private ItemRepository itemRepository;

    public List&lt;Code&gt; importItems() {

        int itemsPerPage = 2000;

        RestPage&lt;Item&gt; restPage = Client.getItems(itemsPerPage, 0);
        List&lt;Item&gt; items = restPage.getContent();
        int totalpages = restPage.getTotalPages();

        List&lt;Item&gt; result = new ArrayList&lt;Item&gt;();
        result.addAll(items);
        for (int pageNumber = 1; pageNumber &lt; totalpages; pageNumber ++) {
            result.addAll(Client.getItems(itemsPerPage, pageNumber).getContent());
        }

        return result.stream()
                .filter(itemo -&gt; !itemRepository.existsByCode(item.getCode()))
                .map(item -&gt; itemRepository.save(new Code(item.getCodemsl)))
                .collect(Collectors.toList());

    }

}

Can you help me in improving this?
I see these lines in your code :
List&lt;Item&gt; items = restPage.getContent();
List&lt;Item&gt; result = new ArrayList&lt;Item&gt;();
result.addAll(items);

You can use the ArrayList(Collection&lt;? extends E&gt; c) constructor obtaining the same result:
List&lt;Item&gt; result = new ArrayList&lt;&gt;(restPage.getContent());

Your filtering code looks good to me and for the for loop adding elements to an existing list for me you made the right choice. Although it is possible to add new elements to an existing list using streams this is discouraged because easy cause of errors, see for example this thread.