\Consumo API
package br.com.alura.TabelaFipe.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 ConsumoApi {
public 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();
}
}
\Converte Dados
package br.com.alura.TabelaFipe.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import java.util.Collection;
import java.util.List;
public class ConvertDados implements IConverteDados{
private ObjectMapper mapper = new ObjectMapper();
@Override
public <T> T obterDados(String json, Class<T> classe) {
try {
return mapper.readValue(json,classe);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Override
public <T> List<T> obeterLista(String json,Class<T> classe) {
CollectionType lista = mapper.getTypeFactory()
.constructCollectionType(List.class,classe);
try {
return mapper.readValue(json,lista);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
\Dados Marca
package br.com.alura.TabelaFipe.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record DadosMarcas(
@JsonAlias("codigo") String codigo,
@JsonAlias("nome") String nome ) { }
\DadosModelo
package br.com.alura.TabelaFipe.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public record DadosModelo(@JsonAlias("modelos") List<DadosMarcas> listModelos) {
}
\Dados Veiculo
package br.com.alura.TabelaFipe.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record DadosVeiculo(
@JsonAlias("TipoVeiculo") Integer tipoVeiculo,
@JsonAlias("Valor") String valor,
@JsonAlias("Marca") String marca,
@JsonAlias("Modelo") String modelo,
@JsonAlias("AnoModelo") Integer anoModelo,
@JsonAlias("Combustivel") String combustivel,
@JsonAlias("CodigoFipe") String codigoFipe,
@JsonAlias("MesReferencia") String mesReferencia,
@JsonAlias("SiglaCombustivel") String siglaCombustivel )
{
@Override
public String toString() {
return "******************************************"+
"\nTipo do Veiculo:" + this.tipoVeiculo +
" Ano Modelo: " + this.anoModelo +
" Modelo: " + this.modelo +
"\n Marca: " + this.marca +
"\n Combustivel: " + this.combustivel+ "-" + this.siglaCombustivel+
"\n Valor Tabela Fipe: " + this.valor+
"\n******************************************";
}
}
\Tabela Fipe package br.com.alura.TabelaFipe;
import br.com.alura.TabelaFipe.principal.Principal;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TabelaFipeApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(TabelaFipeApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Principal principal = new Principal();
principal.exibeMenu();
}
}