I want to get an Array of Objects via GET from my service.
我想从我的服务中通过GET获取一个对象数组。
So I tried:
所以我尝试过:
reader.setEntityClass(ObjectDTO[].class);
res = client.get();
List<ObjectDTO> objects = (List<ObjectDTO>) res.getEntity();
Error:
错误:
Object; cannot be cast to java.util.List
Service:
服务:
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<ObjectDTO> getObjects();
My dependencies:
我的依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.6.0</version>
</dependency>
Full Stacktrace:
完整Stacktrace:
org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with reading the response message, class : class javax.ws.rs.core.Response, ContentType : application/xml.
at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:614)
at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:452)
at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:782)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:766)
at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:696)
at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:242)
at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:268)
at com.bachelor.cxf.CxfClientTest.getEstatesWebClient(CxfClientTest.java:313)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.cxf.jaxrs.client.ClientWebApplicationException: No reader for Response entity java.util.List
at org.apache.cxf.jaxrs.client.ResponseReader.readFrom(ResponseReader.java:92)
at org.apache.cxf.jaxrs.client.ResponseReader.readFrom(ResponseReader.java:77)
at org.apache.cxf.jaxrs.client.ResponseReader.readFrom(ResponseReader.java:40)
at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:450)
... 30 more
EDIT:
编辑:
I tried another approach as well:
我尝试了另一种方法:
List<ObjectDTO> objects = (List<ObjectDTO>) client.get(ObjectDTO.class);
and I get:
我得到:
1435 [main] WARN org.apache.cxf.jaxrs.provider.AbstractJAXBProvider - javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"objectDTOes"). Expected elements are <{}objectDTO>]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:425)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:332)
at org.apache.cxf.jaxrs.provider.JAXBElementProvider.unmarshalFromInputStream(JAXBElementProvider.java:258)
at org.apache.cxf.jaxrs.provider.JAXBElementProvider.doUnmarshal(JAXBElementProvider.java:215)
at org.apache.cxf.jaxrs.provider.JAXBElementProvider.readFrom(JAXBElementProvider.java:182)
at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:450)
at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:782)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:766)
at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:696)
at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:324)
at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:435)
at com.bachelor.cxf.CxfClientTest.getEstatesWebClient(CxfClientTest.java:314)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"objectDTOes"). Expected elements are <{}objectDTO>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:242)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
... 35 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"objectDTOes"). Expected elements are <{}objectDTO>
... 45 more
2 个解决方案
#1
4
Yay, I didnt noticed the getCollection Method. Now it is just:
是的,我没注意到getCollection方法。现在只是:
(List<ObjectDTO>) client.getCollection(ObjectDTO.class);
It works fine.
它工作正常。
#2
1
Change:
更改:
reader.setEntityClass(Object[].class);
to:
至:
reader.setEntityClass(java.util.List.class);
#1
4
Yay, I didnt noticed the getCollection Method. Now it is just:
是的,我没注意到getCollection方法。现在只是:
(List<ObjectDTO>) client.getCollection(ObjectDTO.class);
It works fine.
它工作正常。
#2
1
Change:
更改:
reader.setEntityClass(Object[].class);
to:
至:
reader.setEntityClass(java.util.List.class);