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
Paul samsotha
source share