0

I use this manual to generate code for calling soap webservice in a Springboot project with a different wsdl file.

I copy generated source into 'util.pbn' package of my project but when I run my program I face to this exception:

com.sun.istack.SAXException2: unable to marshal type "util.pbn.RateRequest" as an element because it is missing an @XmlRootElement annotation
at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:948) ~spring-oxm-5.3.20.jar:5.3.20
at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:713) ~spring-oxm-5.3.20.jar:5.3.20
at org.springframework.ws.support.MarshallingUtils.marshal(MarshallingUtils.java:79) ~spring-ws-core-3.1.3.jar:na
at org.springframework.ws.client.core.WebServiceTemplate$2.doWithMessage(WebServiceTemplate.java:402) ~spring-ws-core-3.1.3.jar:na
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:572) ~spring-ws-core-3.1.3.jar:na
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:542) ~spring-ws-core-3.1.3.jar:na
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:394) ~spring-ws-core-3.1.3.jar:na
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383) ~spring-ws-core-3.1.3.jar:na
... 5 common frames omitted

In my generated code, no xmlrootelemet set but there is a complete ObjectFactory in my 'util.pbn' package. I use this block of code to config marshaller but it seems jax2marshaler is not aware of ObjectFactory class.

@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("util.pbn");
    return marshaller;
}

I don't know why jaxb2marshaller dosn't use ObjectFactory class and insists to use @XmlRootElement annotation? How I should config my project to set Objectfactory for jax2bmarshaller?

p.s: I've searched stackoverflow before I post this issue, but none of their solution works for me.

slindenau
  • 1,091
  • 2
  • 11
  • 18
MTB
  • 405
  • 1
  • 5
  • 12
  • Doesn't matter if there is an `ObjectFactory` or not the annotation needs to be there. Instead you need to wrap your response (and request) in a `JaxbElement` so it knows the namespace and element to use. – M. Deinum May 31 '22 at 13:40
  • Have you checked the [following](https://stackoverflow.com/questions/42582550/want-xmlrootelement-in-classes-generated-by-jaxb2-maven-plugin) [questions](https://stackoverflow.com/questions/819720/no-xmlrootelement-generated-by-jaxb/10613686#10613686)? If so, please list what you've already tried that didn't work. – slindenau Jun 06 '22 at 14:12

0 Answers0