OBS: Neste desafio tomei liberdade para escrever dois tipos de arquivos, um em formato JSON e outro em String mesmo. Json no arquiv nomeado "JsonJson" e o formato String no arquivo "JsontoString".
package challenge.services;
public class CepInfo {
//variables
private String cep;
private String logradouro;
private String bairro;
private String localidade;
private String uf;
private String ddd;
//getters and setters
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getLogradouro() {
return logradouro;
}
public void setLogradouro(String logradouro) {
this.logradouro = logradouro;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getLocalidade() {
return localidade;
}
public void setLocalidade(String localidade) {
this.localidade = localidade;
}
public String getUf() {
return uf;
}
public void setUf(String uf) {
this.uf = uf;
}
public String getDdd() {
return ddd;
}
public void setDdd(String ddd) {
this.ddd = ddd;
}
//constructor
public CepInfo(String cep, String logradouro, String bairro, String localidade, String uf, String ddd) {
this.cep = cep;
this.logradouro = logradouro;
this.bairro = bairro;
this.localidade = localidade;
this.uf = uf;
this.ddd = ddd;
}
@Override
public String toString() {
return "Cep: " +cep+ "\n"+
"Logradouro: " +logradouro+ "\n"+
"Bairro: " +bairro+ "\n"+
"Localidade: " +localidade+ "\n"+
"Uf: " +uf+ "\n"+
"DDD: " +ddd+ "\n";
}
}
package challenge.controller;
import challenge.services.CepInfo;
import com.google.gson.*;
import java.io.*;
import java.net.*;
import java.net.http.*;
import java.util.*;
public class ApiCep {
Scanner scanner = new Scanner(System.in);
private String search = scanner.nextLine();
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
public CepInfo apiCepInfo() throws IOException, InterruptedException{
String url = "https://viacep.com.br/ws/" +search+ "/json/";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 400) {
System.out.println("Error! Zip code not found!");
}
//writing in json
System.out.println("Status code: " +response.statusCode());
FileWriter fileJson = new FileWriter("JsonJson");
fileJson.write(response.body());
fileJson.close();
System.out.println("File has been written successfully");
//writing in different archive toString
CepInfo cepInfo = gson.fromJson(response.body(), CepInfo.class);
FileWriter fileToString = new FileWriter("JSONtoString");
fileToString.write(cepInfo.toString());
fileToString.close();
System.out.println("File has been written successfully");
return cepInfo;
}
}
package challenge.main;
import challenge.controller.ApiCep;
import challenge.services.CepInfo;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Enter your cep to check: ");
ApiCep apiCep = new ApiCep();
try {
CepInfo cepInfo = apiCep.apiCepInfo();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Saída esperada:
Enter your cep to check:
79072-460
Status code: 200
File has been written successfully
File has been written successfully