Classe principal
import configuraçoes.Busca;
import java.io.IOException;
import java.util.Scanner;
public class Principal {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner entrada = new Scanner(System.in);
String cep = entrada.nextLine();
Busca busca1 = new Busca(cep);
busca1.MostraCep();
}
}
Classe para fazer a requisição
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 {
private String cep;
private String endereco = "https://viacep.com.br/ws/" + cep + "/json/";
public Busca(String cep) throws IOException, InterruptedException {
this.cep = cep;
}
public void MostraCep () {
System.out.println(response.body());
}
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endereco))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
}