JAXB: How can a returned object be returned as an XML element of a base type?

I write web services using jax-ws, and wsdl contains many types. Java classes for XML mapps using jaxb. Consider the following example:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person")
public class Person {    
    @XmlElement(name = "firstName")
    protected String firstName;    
    @XmlElement(name = "lastName")
    protected String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String value) {
        this.firstName = value;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String value) {
        this.lastName = value;
    }
}

So, we have the XML type Person, which is included in the WSDL. We can call this class an interface (for web service clients). Server code can have a set of derived classes. For instance,

public class EmployeePerson extends Person {
    protected String post;
}

public class ClientPerson extends Person {
}

The web service operation returns an object of type Person:

@WebMethod(operationName = "GETPERSON")
public Person getPerson(@WebParam(name = "ID") String personId);

In fact, we can return one of the derived types, but the XML type "Person" must be returned to the client ( xsi:type='ns:Person'). If we use @XmlTransient, an object of type undefined will be returned. How can we superimpose a derived class on a base type?

+3
source share
2

Person? , ,

someEmployeePerson

Person.

EDIT: :

Person

public class Person {
        public Person() {...}
        public Person(String name) {...}
        public String getName() {...}
        public void setName(String name) {...}

        private String name;
}

ClientPerson

public class ClientPerson extends Person {
        public ClientPerson() {...}
        public ClientPerson(String name, Double payment) {...}
        public Double getPayment() {...}
        public void setPayment(Double payment) {...}

        private Double payment;
}

-

@WebService()
public class testePerson {

        @WebMethod
        public Person getPeople() {
                return new ClientPerson("Some Name", 100.0);
        }
}

example.TestePersonService service = new example.TestePersonService();
example.TestePerson port = service.getTestePersonPort();
Person p = port.getPeople();

, Person, object. WSDL Person.

+1

, . "xsi: type" -, WSDL. , , , . @XmlTransient , .

0

All Articles