java.lang.ClassCastException:无法转换为javax.xml.bind.JAXBElement

use*_*236 10 spring jaxb unmarshalling spring-boot

我使用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;
}
Run Code Online (Sandbox Code Playgroud)

我打电话给服务如下:

    OutputMessageType response = (OutputMessageType) getWebServiceTemplate().marshalSendAndReceive(
            inputMessageType, new SoapActionCallback("http://Serviceuri"));
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

   [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
Run Code Online (Sandbox Code Playgroud)

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

Phi*_*ppe 20

有趣我刚刚有同样的问题,这就是我做的:

将您的回复转换为

JAXBElement<OutputMessageType>
Run Code Online (Sandbox Code Playgroud)

结果就是这样

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

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


fab*_*lin 6

如果今天仍然有用,可以在配置文件中解决此问题。而不是使用的.setPackagesToScan在下面一行:marshaller.setPackagesToScan("com.client.stub")
考虑使用.setClassesToBeBound
但是,你需要你的包下引用每个类(将被用于和解组):

   marshaller.setClassesToBeBound(new Class[] { 
       com.client.stub.Foo.class,
       com.client.stub.Bar.class,
       com.client.stub.Baz.class
   });
Run Code Online (Sandbox Code Playgroud)

无需强制转换为JAXBElement。