//Realizei o projeto final com 2 formas de geração do arquivo //1-Com record //2-Sendo passado diretamente o response para o arquivo //Class Geraarquivo
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
import java.net.http.HttpResponse;
public class Gera_Arquivo {
public void geraArquivo_Rec(Rec_Endereco endereco) throws IOException {
Gson json = new GsonBuilder().setPrettyPrinting().create();
FileWriter escrita = new FileWriter(endereco.cep()+".json");
escrita.write(json.toJson(endereco)) ;
escrita.close();
}
public void geraArquivo_String(String endereco, String cep) throws IOException {
Gson json = new GsonBuilder().setPrettyPrinting().create();
FileWriter escrita = new FileWriter(cep+".json");
escrita.write(endereco) ;
escrita.close();
}
}
//Records
public record Rec_Endereco(String cep,String logradouro,String complemento,String bairro,
String localidade,String uf,String ibge,String ddd ) {
}
//Busca CEP
import com.google.gson.Gson;
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 Busca_Cep {
public Rec_Endereco pesquisaCep(String cep) throws IOException, InterruptedException {
//Consumir API
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://viacep.com.br/ws/" + cep + "/json/")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return new Gson().fromJson(response.body(), Rec_Endereco.class);
}
public HttpResponse<String> pesquisaCep_resp(String cep) throws IOException, InterruptedException {
//Consumir API
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://viacep.com.br/ws/" + cep + "/json/")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
}
//Principal -> Main
import com.google.gson.Gson;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class Principal {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner leitura = new Scanner(System.in);
System.out.println("**************************");
System.out.println("**********Busca CEP*******");
System.out.println("**************************");
System.out.println("Informe um CEP a ser consultado.:");
String cep= leitura.next();
try {
Busca_Cep buscaCep = new Busca_Cep();
//Utilizando Registro
Rec_Endereco endereco = buscaCep.pesquisaCep(cep);
//Gerando arquivo Json com Registro
Gera_Arquivo arquivo = new Gera_Arquivo();
arquivo.geraArquivo_Rec(endereco);
//Sendo gerado arquivo gson diretamento do response
arquivo.geraArquivo_String(buscaCep.pesquisaCep_resp(cep).body(), cep);
}catch (RuntimeException | IOException e) {
System.out.println(e.getMessage());
System.out.println("Aplicação Finalizada");
}
}
}