bug query get with version 4
GeorgeSalu opened this issue · 5 comments
This query works in version 3.14.5
`
Todo books = Unirest.get("https://jsonplaceholder.cypress.io/todos/1")
.asObject(Todo.class)
.getBody();
`
but in version 4.0.12 it throws this error
`
Exception in thread "main" java.util.ServiceConfigurationError: kong.unirest.core.json.JsonEngine: kong.unirest.gson.GsonEngine Unable to get public no-arg constructor
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:582)
at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:673)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1233)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1265)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1300)
at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1385)
at java.base/java.util.ServiceLoader.findFirst(ServiceLoader.java:1809)
at kong.unirest.core.json.CoreFactory.(CoreFactory.java:36)
at kong.unirest.core.Config.lambda$setDefaults$1(Config.java:99)
at kong.unirest.core.Config.getObjectMapper(Config.java:730)
at java.base/java.util.Optional.orElseGet(Optional.java:369)
at kong.unirest.core.BaseRequest.getObjectMapper(BaseRequest.java:409)
at kong.unirest.core.BaseRequest.lambda$asObject$6(BaseRequest.java:242)
at kong.unirest.core.java.JavaClient.transformBody(JavaClient.java:160)
at kong.unirest.core.java.JavaClient.request(JavaClient.java:72)
at kong.unirest.core.BaseRequest.request(BaseRequest.java:341)
at kong.unirest.core.BaseRequest.asObject(BaseRequest.java:242)
at unirest.TesteMain.main(TesteMain.java:9)
Caused by: java.lang.NoClassDefFoundError: com/google/gson/TypeAdapter
at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
at java.base/java.lang.Class.getConstructor0(Class.java:3342)
at java.base/java.lang.Class.getConstructor(Class.java:2151)
at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:660)
at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:657)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:668)
... 16 more
Caused by: java.lang.ClassNotFoundException: com.google.gson.TypeAdapter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527)
... 24 more
`
Have you included the desired json library in your build? Please see the Readme and docs, json is no longer automatically added. You have to include it
includes the following dependencies
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.konghq/unirest-java-bom -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java-bom</artifactId>
<version>4.0.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.konghq/unirest-java-core -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java-core</artifactId>
</dependency>
<!-- pick a JSON module if you want to parse JSON include one of these: -->
<!-- Google GSON -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-object-mappers-gson</artifactId>
</dependency>
</dependencies>
I have to include something but ????
huh, so, apparently doing it like this does not make maven include GSON even though it is a full dependency. I'll work on figuring out why, but, what you need to do for now is to include gson itself.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
this should be fixed in 4.1.0 without needing the extra gson inport
yes with version 4.1.0 now it adds the gson dependency together, thank you