Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to

I am using Spring boot to call a webservice.

my config class is as:

@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;
}

i am calling service as following:

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

I am getting following error:

   [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

How to unMarshal the output from webservice????? How do i set unmarshaller for the response??

like image 687
user5636236 Avatar asked Mar 18 '16 15:03

user5636236


1 Answers

Funny I JUST had the same issue, here's what I did :

Cast your response to a

JAXBElement<OutputMessageType>

So the result would be

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

I have about the same configurations as you. I'm still trying to figure out why there's a ClassCastException. At least we have a workaround...

like image 58
Philippe Avatar answered Sep 19 '22 12:09

Philippe