org.codehaus.jackson.map.JsonMappingException:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
我开发了一个 Spring MVC+ RestEasy+Hibernate 示例,并希望使用 Junit 对其进行测试,但看到以下错误:我也可以通过 jsp 或 rest 客户端调用这些 Web 服务。
org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream@23c30a20; line: 1, column: 1]
at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:470)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:385)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:346)
at test.resteasy.series.spring.mvc.hibernate.service.CustomerTest.testGetallcustomer(CustomerTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream@23c30a20; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:51)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:434)
... 26 more
这是我开发的Junit测试:
public class CustomerTest {
static final String ROOT_URL = "http://localhost:8080/RestEasy-Spring-MVC-Hibernate/resteasy/";
@Test
public void testGetallcustomer() throws Exception {
ClientRequest request = new ClientRequest(ROOT_URL+"customerservice/getallcustomer");
ClientResponse<List<Customer>> response = request.get(new GenericType<List<Customer>>(){});
List<Customer> customers = response.getEntity();
System.out.println("SIZE : "+customers.size());
}
}
看起来 API 也已弃用:
我并不是说这是解决问题的方法。这是另一种解决问题的方法。
public class CustomerTest {
final String path = "http://localhost:8080/RestEasy-Spring-MVC-Hibernate/";
ICustomerService proxy = null;
@Before
public void beforeClass(){
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath(path));
proxy = target.proxy(ICustomerService.class);
}
@Test
public void testGetallcustomer() throws Exception {
CustomerListType customers = proxy.getAllCustomerInfo();
List<CustomerType> customerTypes = customers.getCustomerType();
for (CustomerType customerType : customerTypes) {
System.out.println("----------------------------------------------------");
System.out.println("Name : "+customerType.getName());
System.out.println("Age : "+customerType.getAge());
System.out.println("CustomerId : "+customerType.getCustomerId());
}
}
}
我开发了一个 Spring MVC+ RestEasy+Hibernate 示例,并希望使用 Junit 对其进行测试,但看到以下错误:我也可以通过 jsp 或 rest 客户端调用这些 Web 服务。
org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream@23c30a20; line: 1, column: 1]
at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:470)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:385)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:346)
at test.resteasy.series.spring.mvc.hibernate.service.CustomerTest.testGetallcustomer(CustomerTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream@23c30a20; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:51)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:434)
... 26 more
这是我开发的Junit测试:
public class CustomerTest {
static final String ROOT_URL = "http://localhost:8080/RestEasy-Spring-MVC-Hibernate/resteasy/";
@Test
public void testGetallcustomer() throws Exception {
ClientRequest request = new ClientRequest(ROOT_URL+"customerservice/getallcustomer");
ClientResponse<List<Customer>> response = request.get(new GenericType<List<Customer>>(){});
List<Customer> customers = response.getEntity();
System.out.println("SIZE : "+customers.size());
}
}
看起来 API 也已弃用:
我并不是说这是解决问题的方法。这是另一种解决问题的方法。
public class CustomerTest {
final String path = "http://localhost:8080/RestEasy-Spring-MVC-Hibernate/";
ICustomerService proxy = null;
@Before
public void beforeClass(){
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath(path));
proxy = target.proxy(ICustomerService.class);
}
@Test
public void testGetallcustomer() throws Exception {
CustomerListType customers = proxy.getAllCustomerInfo();
List<CustomerType> customerTypes = customers.getCustomerType();
for (CustomerType customerType : customerTypes) {
System.out.println("----------------------------------------------------");
System.out.println("Name : "+customerType.getName());
System.out.println("Age : "+customerType.getAge());
System.out.println("CustomerId : "+customerType.getCustomerId());
}
}
}