WebService returning subclasses

Source: Internet
Author: User

Wrote a simple webservice with CXF.

The model is as follows:

public class ProductModel implements Icachable, Serializable {...}

public class Invelinkmodel extends ProductModel {...}

The WebService interface is defined as follows

Public interface Iproductcenter {

Public ProductModel seekpinfo (String pid);

}

A client was acquired with spring injection. The XML configuration for spring is as follows:

<jaxws:client id= "pcenterclient" address= "" serviceclass= "xxx"/>

The client code is as follows:

Iproductcenter client;

There's a problem here.

ProductModel Product = Client.seekpinfo ("eli_cw_001");

The problem is that the model that the client obtains is always an instance of ProductModel, regardless of whether the WebService service-side implementation class returns Invelinkmodel or ProductModel.

The workaround is simple:

Import javax.xml.bind.annotation.XmlSeeAlso;

@XmlSeeAlso (value = {Invelinkmodel.class})
public class ProductModel implements Icachable, Serializable {}

The xmlseealso annotation declares a subclass of this class. The WebService server is serialized and deserialized based on the value of this annotation and the actual type of model, allowing the client to obtain the correct type required.

WebService returning subclasses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.