Search code examples
javaweb-servicesjaxbjax-ws

JAX-WS how to return custom class object from 3rd party library


I am writing a JAX-WS web services that should return custom object that is a party of 3rd party library.

The example of method is:

@WebMethod
public CustomObject create(CustomObject2 object)

I am not able to modify CustomObject as it is part of 3rd party library. Is there any simple way how to change this to have JAX-B-compatible parameters and return types?


Solution

  • If CustomObject is a POJO, you can use inheritance to add the JAXB annotations, and apache commons BeanUtils.copyProperties to clone the properties to the JAXB object

    CustomObjectJAXB

    @XmlRootElement(name = "CustomObject")
    public class CustomObjectJAXB extends CustomObject{
    

    WebMethod

    @WebMethod
    public CustomObject create(CustomObject2 object)
       CustomObject co = ... // Get CustomObject from third party library
    
       CustomObjectJAXB coJaxb = new CustomObjectJAXB()
       BeanUtils.copyProperties (coJaxb,co);
       return coJaxb;
    }
    

    This solutions is simply and probably works fine with plain objects, but if CustomObject is complex (contains lists, maps, or other objects), the resultant xml probably won't be very orthodox