package br.com.meuprojeto.test;
import br.com.meuprojeto.error.ExeptionsError;
import br.com.meuprojeto.model.TituleOmdb;
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;
import java.util.Scanner;
public class Myrequest {
public static void main(String[] args) throws IOException, InterruptedException {
// chamando a requisicao
Scanner scanner = new Scanner(System.in);
System.out.println("pesquise um filme");
var busca = scanner.nextLine();
String api = "https://www.omdbapi.com/?t="+busca+"&apikey=c4c55275";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(api)).build();
HttpResponse response = client.send(request,HttpResponse.BodyHandlers.ofString());
// Gson json = new Gson();
var resps = (String) response.body();
// Titulo resp = json.fromJson(resps,Titulo.class);
Gson json = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
TituleOmdb titulo = json.fromJson(resps, TituleOmdb.class);
System.out.println(resps);
System.out.println(titulo);
}
}
package br.com.meuprojeto.model;
public record TituleOmdb(String title, String year, String runtime) {
}
quando vou compilar o projeto esta aparecendo essa exeption!
Exception in thread "main" java.lang.AssertionError: java.lang.IllegalAccessException: Can not set final java.lang.String field br.com.meuprojeto.model.TituleOmdb.title to java.lang.String
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at com.google.gson.Gson.fromJson(Gson.java:888)
at com.google.gson.Gson.fromJson(Gson.java:853)
at com.google.gson.Gson.fromJson(Gson.java:802)
at com.google.gson.Gson.fromJson(Gson.java:774)
at br.com.meuprojeto.test.Myrequest.main(Myrequest.java:32)
Caused by: java.lang.IllegalAccessException: Can not set final java.lang.String field br.com.meuprojeto.model.TituleOmdb.title to java.lang.String
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:80)
at java.base/jdk.internal.reflect.UnsafeQualifiedObjectFieldAccessorImpl.set(UnsafeQualifiedObjectFieldAccessorImpl.java:79)
at java.base/java.lang.reflect.Field.set(Field.java:799)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
... 5 more