Deserializing JSON with Jersey and MOXy to List List

I am trying to take JSON objects and put them in a collection (I selected List). I was able to decouple the JSON responses into separate POJOs by creating BuiltCharacter with the corresponding getters and setters. For an array of JSON elements, I tried the following approaches:

List<BuiltCharacter> characters = response.readEntity(new GenericType<List<BuiltCharacter>>(){}); 

and

 List<BuiltCharacter> characters = client.target(uri).request(MediaType.APPLICATION_JSON).get(new GenericType<List<BuiltCharacter>>(){}); 

Using these approaches, I got the following:

Exception in thread "main" java.lang.ClassCastException: BuiltCharacter cannot be added to java.util.List

I originally used Character as the class name, but I was reminded that this is a reserved class name. However, I cannot understand what is happening here!

+7
java json jersey moxy
source share
1 answer

Respond to generosity, but this seems to be the same problem from the original message

Looking at the link you provided, all examples return SalesType , not List<SalesType> . You cannot expect SalesType be converted to List<SalesType> . If you return SalesType from your resource class, an exception is thrown.

Even from the link for the original message, JSON comes in as a JSON object. List not mapped to a JSON ( {} ) object, but instead a JSON ( [] ) array.

If you need a SalesType list, just return the list from the resource. Below is a complete example

 import java.util.*; import javax.ws.rs.*; import javax.ws.rs.core.*; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.Test; class SalesType { public String test; } public class MOXyTest extends JerseyTest { @Path("test") public static class TestResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getSalesTypeList() { List<SalesType> list = new ArrayList<>(); SalesType sales = new SalesType(); sales.test = "test"; list.add(sales); list.add(sales); return Response.ok( new GenericEntity<List<SalesType>>(list){}).build(); } } @Override public Application configure() { return new ResourceConfig(TestResource.class); } @Test public void testGetSalesTypeList() { List<SalesType> list = target("test").request() .get(new GenericType<List<SalesType>>(){}); for (SalesType sales: list) { System.out.println(sales.test); } } } 

These are the two dependencies that I used (using the Jersey Test Model )

 <dependency> <groupId>org.glassfish.jersey.test-framework.providers</groupId> <artifactId>jersey-test-framework-provider-inmemory</artifactId> <version>${jersey2.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>${jersey2.version}</version> </dependency> 

Your problem can be easily reproduced by returning sales in the resource class from the above example. The current code above works, but just to see the error, if you replace GenericEntity (which is a wrapper for typical types returned in Response ), you will see the same error

 java.lang.ClassCastException: com.stackoverflow.jersey.test.SalesType cannot be cast to java.util.List 
+2
source share

All Articles