Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB cannot handle an interface - what am I missing?

I'm getting familiar with web services in Java using Jax-ws (or JAXB, not sure, anyway...).

I've created small project with a single webservice. The WS has the only endpoint called transfer and returns objects inheriting ITransferResult interface.

Web service contract

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface IBankWebSrv {
    @WebMethod
    ITransferResult transfer(String accountNumber, double amount);
}

Web service implementation

//Service Implementation
@WebService(endpointInterface = "Contracts.IBankWebSrv")
public class BankWebSrv implements IBankWebSrv {
    @Override
    public ITransferResult transfer(String accountNumber, double amount) {
        ITransferResult result = new TransferResult();
        // TODO logic here
        result.setSuccessful(true);
        return result;
    }
}

TransferResult contract

@XmlJavaTypeAdapter(TransferResult.class)
public interface ITransferResult {
    boolean isSuccessful();
    void setSuccessful(boolean successful);
}

TransferResult implementation

public class TransferResult extends XmlAdapter<TransferResult, ITransferResult>
        implements ITransferResult {

    @XmlElement
    boolean successful;

    public boolean isSuccessful() {
        return this.successful;
    }

    public void setSuccessful(boolean successful) {
        this.successful = successful;
    }

    @Override
    public TransferResult marshal(ITransferResult v) throws Exception {
        return (TransferResult) v;
    }

    @Override
    public ITransferResult unmarshal(TransferResult v) throws Exception {
        return (ITransferResult) v;
    }
}

When I publish my web service, I get the following error:

Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create JAXBContext...

Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions ITransferResult is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at ITransferResult

I've looked over SO for the answer and applied to the most repetitive tips, but none of them have worked for me yet.

What am I missing?

like image 846
pizycki Avatar asked Apr 21 '14 11:04

pizycki


1 Answers

You may need to change the style to be DOCUMENT instead of RPC in your declaration at @SOAPBinding(style = Style.RPC)

Although this is an old question, I thought I'd answer it as it's common exception people encounter.

The difference between the two styles in high level is as follows

  • Document: The return type and method arguments are clearly explained in a separate XSD with each type in detail - helpful in case of custom data types (Example in your case ITransferResult or java.util.List).

  • RPC: the types are defined in the WSDL itself in simple manner.

like image 73
Santosh Kumar Arjunan Avatar answered Sep 28 '22 08:09

Santosh Kumar Arjunan