Request for Resttemplate spring

Asked

Viewed 47 times

0

I am trying to make a request for a get type starWars api to the following url = https://swapi.dev/api/planets/3

The codes stayed that way:

@Log4j2
public class SpringClient {
    public static void main(String[] args) {


        ResponseEntity<List<Planet>> exchange = new RestTemplate().exchange("https://swapi.dev/api/planets/3", HttpMethod.GET, null,
                new ParameterizedTypeReference<List<Planet>>() {
                });
        log.info(exchange.getBody());
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Planet implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;

    private String name;

    private String rotation_period;

    private String orbital_period;

    private String diameter;

    private String climate;

    private String gravity;

    private String terrain;

    private String surface_water;

    private String population;

    @ElementCollection
    private List<String> residents;

    @ElementCollection
    private List<String> films;

    private String created;

    private String edited;

    private String url;


}

But he makes the following mistake:

22:49:35.318 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://swapi.dev/api/planets/3
22:49:35.462 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/json, application/*+json]
22:49:36.833 [main] DEBUG org.springframework.web.client.RestTemplate - Response 301 MOVED_PERMANENTLY
22:49:36.847 [main] INFO teste.apistarwars.client.SpringClient - null

When I make the request by Insomnia passing only the url of the api, it gives me the JSON return straight, but by restTemplate is giving this error of 301 MOVED_PERMANENTLY

Thank you in advance, guys.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.