4

サービスからGETを介してオブジェクトの配列を取得したい。

だから私は試しました:

 reader.setEntityClass(ObjectDTO[].class); 
 res = client.get();    
 List<ObjectDTO> objects = (List<ObjectDTO>) res.getEntity();

エラー:

 Object; cannot be cast to java.util.List

サービス:

    @GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<ObjectDTO> getObjects();

私の依存関係:

    <dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxrs</artifactId>
  <version>2.6.0</version>
</dependency>

完全なスタックトレース:

        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

編集:

私も別のアプローチを試しました:

 List<ObjectDTO> objects = (List<ObjectDTO>) client.get(ObjectDTO.class);

そして私は得る:

    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
4

2 に答える 2

4

はい、getCollectionメソッドに気づきませんでした。今それはただです:

 (List<ObjectDTO>) client.getCollection(ObjectDTO.class);

正常に動作します。

于 2012-07-18T17:39:25.480 に答える
2

変化する:

reader.setEntityClass(Object[].class); 

に:

reader.setEntityClass(java.util.List.class); 
于 2012-07-18T09:08:56.547 に答える