spring – java.lang.ClassCastException:javax.xml.bind.JAXBElement无法强制转换为

我使用Spring启动来调用web服务.

我的配置类如下:

@Configuration
public class ClientAppConfig {
@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan("com.client.stub");
    return marshaller;
}

@Bean
public ARTestClient arTestClient(Jaxb2Marshaller marshaller) {
    ARTestClient client = new ARTestClient();
    client.setDefaultUri("uri");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
   return client;
}

我打电话给服务如下:

    OutputMessageType response = (OutputMessageType) getWebServiceTemplate().marshalSendAndReceive(
            inputMessageType, new SoapActionCallback("http://Serviceuri"));

我收到以下错误:

   [2016-03-18 14:45:43.697] boot - 10272 ERROR [http-nio-8080-exec-1] --- [dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
  [Request processing failed; nested exception is java.lang.ClassCastException: javax.xml.bind.JAXBElement 
  cannot be cast to com.wfs.client.stub.ar.OutputMessageType] with root cause

如何解析webservice的输出?????
我如何为响应设置unmarshaller?

最佳答案
有趣我刚刚有同样的问题,这就是我做的:

将您的回复转换为

JAXBElement<OutputMessageType>

结果就是这样

JAXBElement<OutputMessageType> response = (JAXBElement<OutputMessageType>) getWebServiceTemplate().marshalSendAndReceive(
        inputMessageType, new SoapActionCallback("http://Serviceuri"));
// Then just call
System.out.println(response.getValue());

我有与你相同的配置.我还在试图弄清楚为什么会有ClassCastException.至少我们有一个解决方法……

转载注明原文:spring – java.lang.ClassCastException:javax.xml.bind.JAXBElement无法强制转换为 - 代码日志