WebResult vs RequestWrapper Annotations with polymophism

I have a class, for example:

@XmlRootElement(namespace = "http://foo.com/model", name = "Person") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(namespace = "http://foo.com/model", name = "Person" public abstract Person { } 

And two classes that inherit from this:

 @XmlRootElement(namespace = "http://foo.com/model", name = "ShortPerson") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(namespace = "http://foo.com/model", name = "ShortPerson" public ShortPerson extends Person { } @XmlRootElement(namespace = "http://foo.com/model", name = "TallPerson") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(namespace = "http://foo.com/model", name = "TallPerson" public TallPerson extends Person { } 

Then I have SEI as such:

 @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED, use = SOAPBinding.Use.LITERAL, style = SOAPBinding.Style.DOCUMENT) @WebService(targetNamespace = "http://foo.com/services") public interface PersonService { @RequestWrapper(localName = "getPersonById", className = "com.foo.services.personService.GetPersonById", targetNamespace = "http://foo.com/services") @ResponseWrapper(localName = "getPersonByIdResponse", className = "com.foo.services.personService.GetPersonByUidResponse", targetNamespace = "http://foo.com/services") @WebResult(targetNamespace = "http://foo.com/model", name = "Person") Person getPersonById( @WebParam(targetNamespace = "http://foo.com/services", name = "PersonId") Long personId); } 

And my response wrapper class is as follows:

 @XmlRootElement(name = "getPersonByIdResponse", namespace = "http://foo.com/services") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "getPersonByIdResponse", namespace = "http://foo.com/services", propOrder = { "person" }) public class GetPersonByIdResponse { @XmlElementRefs( { @XmlElementRef(name = "TallPerson", type = TallPerson.class, namespace = "http://foo.com/model"), @XmlElementRef(name = "SmallPerson", type = SmallPerson.class, namespace = "http://foo.com/model") }) private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 

}

So the problem is that if I directly test ResponseWrapper through JAXB and populate the TallPerson object, I will correctly marshal this object in XML, which is specific to TallPerson (and the same thing works for SmallPerson).

The problem is when I deploy it during the war and the service, turn it on when the result is returned by TallPerson, I get nothing from the web service call except an empty message. Actually, I don’t even get the XML element of the root root of the response from <getPersonByIdResponse>.

So my problem is that if I have a polymorphic request or response object (in this case, the response object), how does the name / targetnamespace WebResult affect the ResponseWrapper parameters. Since in my case I am using XmlElementRefs to properly marshal my base Person object in TallPerson or SmallPerson, but how does this relate to the namespace / namespace of the parent class Person in WebResult? I'm not sure if the conflict causes the XML response to not be generated.

thanks

+4
source share
1 answer

Make sure all your classes are encoded in a JAXB context by annotating the service with XmlSeeAlso:

 @WebService @XmlSeeAlso({TallPerson.class,SmallPerson.class}) public interface PersonService { @WebResult(name = "Person") Person getPersonById( @WebParam(name = "PersonId") Long personId); } 

And you do not need to play with wrappers. JAX-WS will handle this, it will also generate the correct WSDL.

0
source

Source: https://habr.com/ru/post/1346104/


All Articles