Spring java.lang.ClassCastException:无法将javax.xml.bind.JAXBElement强制转换为
我正在使用SpringBoot调用Web服务 我的配置类如下所示:Spring java.lang.ClassCastException:无法将javax.xml.bind.JAXBElement强制转换为,spring,jaxb,spring-boot,unmarshalling,Spring,Jaxb,Spring Boot,Unmarshalling,我正在使用SpringBoot调用Web服务 我的配置类如下所示: @Configuration public class ClientAppConfig { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("com.client.stub"); return marsha
@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
所以结果是
JAXBElement<OutputMessageType> response = (JAXBElement<OutputMessageType>) getWebServiceTemplate().marshalSendAndReceive(
inputMessageType, new SoapActionCallback("http://Serviceuri"));
// Then just call
System.out.println(response.getValue());
JAXBElement响应=(JAXBElement)getWebServiceTemplate().MarshalSendReceive(
inputMessageType,新的SoapActionCallback(“http://Serviceuri"));
//那就打电话吧
System.out.println(response.getValue());
我的配置和你差不多。我还在想为什么会有ClassCastException。至少我们有一个解决方法…如果它今天仍然相关,那么在配置文件中有一种方法可以解决这个问题。而不是在以下行中使用.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
});
无需强制转换到JAXBElement。JAXBElement响应=(JAXBElement)
JAXBElement<OutputMessageType> response = (JAXBElement<OutputMessageType>)
getWebServiceTemplate().marshalSendAndReceive(
inputMessageType, new SoapActionCallback("http://Serviceuri"));
// Then just call
System.out.println(response.getValue());
getWebServiceTemplate().MarshalSendReceive(
inputMessageType,新的SoapActionCallback(“http://Serviceuri"));
//那就打电话吧
System.out.println(response.getValue());
对我的案子很有效那么你没有未经检查的警告吗?这就是我的问题所在。我没有在pom.xml中为生成的代码指定jaxb库版本。2年后,我重新生成了代码,Jaxb的最新版本启动了,我的代码给了我这个神秘的错误。非常感谢。谢谢它确实有帮助,请在回答中添加更多内容,解释OP为什么应该使用您的代码以及它如何解决他们的问题。