Fiz algumas alteraçoes no código da aula, ao invés de instanciar os objetos e usar uma interface, transformei os métodos em estáticos
package br.com.alura.Screenmatch;
import br.com.alura.Screenmatch.model.DadosSerie;
import br.com.alura.Screenmatch.service.ApiRequest;
import br.com.alura.Screenmatch.service.DataConverter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ScreenmatchApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ScreenmatchApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
var API_KEY = System.getenv("API_KEY");
var json = ApiRequest.obterDados("http://www.omdbapi.com/?t=Supernatural&apikey="+API_KEY);
DadosSerie dados = DataConverter.getData(json,DadosSerie.class);
System.out.println(dados);
}
}
package br.com.alura.Screenmatch.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DataConverter {
private static ObjectMapper mapper = new ObjectMapper();
static public <T> T getData(String json, Class<T> classe) {
try {
return mapper.readValue(json,classe);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
package br.com.alura.Screenmatch.service;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class ApiRequest {
public static String obterDados(String endereco) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endereco))
.build();
HttpResponse<String> response = null;
try {
response = client
.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
return response.body();
}
}