'Multiple Get Requests with Spring Boot, WebClient and Thymeleaf

I'm trying to make a RESTClient application for consuming the Starwars API. I take the following URL: https://swapi.py4e.com/api/films/

The user logs in my website and can search for a film (introducing the id in a form). Later, the website shows title, director, release date and a the list of characters.

For instance, if the user inserts 1 (https://swapi.py4e.com/api/films/1), he should get the following info on screen:

{
    "title": "A New Hope", 
    "episode_id": 4,
    "director": "George Lucas",
    "characters": [
        "https://swapi.py4e.com/api/people/1/", 
        "https://swapi.py4e.com/api/people/2/", 
        "https://swapi.py4e.com/api/people/3/", 
        "https://swapi.py4e.com/api/people/4/", 
        "https://swapi.py4e.com/api/people/5/", 
        "https://swapi.py4e.com/api/people/6/", 
        "https://swapi.py4e.com/api/people/7/", 
        "https://swapi.py4e.com/api/people/8/", 
        "https://swapi.py4e.com/api/people/9/", 
        "https://swapi.py4e.com/api/people/10/", 
        "https://swapi.py4e.com/api/people/12/", 
        "https://swapi.py4e.com/api/people/13/", 
        "https://swapi.py4e.com/api/people/14/", 
        "https://swapi.py4e.com/api/people/15/", 
        "https://swapi.py4e.com/api/people/16/", 
        "https://swapi.py4e.com/api/people/18/", 
        "https://swapi.py4e.com/api/people/19/", 
        "https://swapi.py4e.com/api/people/81/"
    ], 

The urls from the characters should show the name of the characters. This should make, I think, an in-between get request to (for example) the urls and stock them in a list, set or array.

For instance: https://swapi.py4e.com/api/people/1/, I only need the name.

{
    "name": "Luke Skywalker", 
}

For that, I have the following code snippets (I'm using Spring Boot, MVC and working with layers, as in an enterprise application):

DefaultSWAPIClient (with an interface SWAPIClient):

@Component
class DefaultSWAPIClient implements SWAPIClient {
    private final WebClient client;
    private final String filmURI;

    DefaultSWAPIClient(
            WebClient.Builder builder,
            @Value("${swapi.films}") String filmURI) {
        client = builder.build();
        this.filmURI = filmURI;
    }


    @Override
    public Optional<Film> findByEpisodeId(long id) {
        try {
            return Optional.of(
                    client.get()
                            .uri(filmURI, uriBuilder -> uriBuilder.build(id))
                            .retrieve().bodyToMono(Film.class).block());
        } catch (WebClientResponseException.NotFound ex) {
            return Optional.empty();
        }
    }
}

DTO (of DAO) film class:

public class Film {
    @JsonProperty("title")
    private String title;
    @JsonProperty("episode_id")
    private long episodeId;
    @JsonProperty("director")
    private String regisseur;
    @JsonProperty("release_date")
    private String releaseDate;
    @JsonProperty("characters")
    private LinkedHashSet<String> characters = new LinkedHashSet<String>();
    
    public String getTitle() {
        return title;
    }

    public long getEpisodeId() {
        return episodeId;
    }

    public String getRegisseur() {
        return regisseur;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public LinkedHashSet<String> getCharacters() {
        return characters;
    }
}

The search film Controller (FilmZoekenController):

@Controller
@RequestMapping("filmzoeken")
class FilmZoekenController {
    private final SWAPIClient client;

    FilmZoekenController(SWAPIClient client) {
        this.client = client;
    }

    @GetMapping
    public ModelAndView toonForm() {
        return new ModelAndView("filmzoeken");
    }

    @GetMapping("/film/{id}")
    public ModelAndView getData(@PathVariable long id) {
        var modelAndView = new ModelAndView("film");
        client.findByEpisodeId(id)
                .ifPresent(film -> modelAndView.addObject(film));
        return modelAndView;
    }
}

Then I show all of it in a thymeleaf template.

I don't understand how to make another GET request to the api before it returns the data.

To sum up: I want to do a get request to films/{id} through a form, get some attributes (one being an array of urls, which need to returns the name value from each direction, so I assume another get request within) and show them to the user.

I hope that this should be enough info/code to help me out! Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source