Não estou conseguindo atribuir valores a classe record, que receberia as informações trazidas do via cep: Abaixo segue o record e o main:
record:
public record CadastroEnderecoViaCep(String cep, String logradouro, String complemento, String bairro, String localidade, String uf, String ibge, String gia, String ddd, String siafi) {
}
main:
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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 Main {
public static void main(String[] args) throws IOException, InterruptedException {
String json;
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).setPrettyPrinting().create();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://viacep.com.br/ws/02124070/json/"))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
// HttpClient client = HttpClient.newHttpClient();
// HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://viacep.com.br/ws/02124070/json/")).build();
// HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// response.body();
json = response.body();
System.out.println(response.body());
CadastroEnderecoViaCep novoViaCep = gson.fromJson(json, CadastroEnderecoViaCep.class);
System.out.println(novoViaCep.toString());
}
}
Poderiam validar aonde estou errando?
o retorno que obtenho ao rodar o main:
"C:\Program Files\Java\jdk-19\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1\lib\idea_rt.jar=51898:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath "C:\Users\leafa\OneDrive\Área de Trabalho\Curso JavaAlura\ApiViaCep2\out\production\ApiViaCep2;C:\Users\leafa\OneDrive\Área de Trabalho\Java Alura\gson-2.10.1.jar" Main { "cep": "02124-070", "logradouro": "Rua Taka", "complemento": "", "bairro": "Jardim Japão", "localidade": "São Paulo", "uf": "SP", "ibge": "3550308", "gia": "1004", "ddd": "11", "siafi": "7107" } CadastroEnderecoViaCep[cep=null, logradouro=null, complemento=null, bairro=null, localidade=null, uf=null, ibge=null, gia=null, ddd=null, siafi=null]
Process finished with exit code 0