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

标签 spring jaxb spring-boot unmarshalling

我正在使用 Spring Boot 来调用 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 的输出????
我如何为响应设置解码器?

最佳答案

有趣的是,我只是遇到了同样的问题,这就是我所做的:

将您的回复转换到

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 无法转换为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36088010/

相关文章:

java - 外部化 Spring 库 Web 应用程序

java - Spring MVC 和 "big forms"

java - 一起使用 Spring Boot、QueryDSL 和 Springfox Swagger - Guava 版本不匹配

java - 找不到属性 : Error in spring boot 的设置方法

java - 使用 JAXB 剪切 XML

java - 响应式(Reactive) Redis 序列化问题。无法序列化类型类 java.lang.Integer 的值,而不在 Redis 中保存时生成序列化器错误

java - 在 Spring Boot 中使用多个调度程序 Servlet/Web 上下文

java - Spring @RequestParam 日期格式化

java - 解码时出现意外的元素错误

java - JAXB 测试手工制作的 bean