简体   繁体   中英

Jax-WS interface as parameter

try to use a interface as parameter for jax-ws web service Interface

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlJavaTypeAdapter(TransactionBase.TransactionableAdapter.class)
public interface Transactionable {

public int getRecid() ;
public void setRecid(int recid) ;
}

Implementation

import com.tr.route.transactions.Transactionable;
@XmlRootElement 
@XmlType(name = "Transactionable")
public  class TransactionBase implements Transactionable{

    private int recid;
    private String ref;
    public TransactionBase(String type ) {
    System.out.println("TransactionBase.Constructor with param:" +       type);
     }

   public static TransactionBase valueOf(String allObject) {
    System.out.println("TransactionBase.valueOf:");
    TransactionBase rezult =  new TransactionBase(allObject);
    return rezult;
   }
    public int getRecid() {
        return recid;
    }

    public void setRecid(int recid) {
        this.recid = recid;
    }
    // adapter
  public static class TransactionableAdapter
    extends XmlAdapter<TransactionBase, Transactionable> {

    @Override
    public TransactionBase marshal(Transactionable o)
        throws Exception {
        System.out.println("TransactionableAdapter:mashal:");           
        return (TransactionBase)o;
    } 

    @Override
    public Transactionable unmarshal(TransactionBase o)
        throws Exception {
        System.out.println("TransactionableAdapter:unmashal:");
    return (Transactionable)o;
    }
    }
}

Web service class

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/ExternalTransactions")
 public class ExternalTransactions {



@POST 
@Produces( MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML) 
@Path("/insertTransaction") 

@XmlElement(type=TransactionBase.class)
public String insertTransaction(Transactionable insertRequest) {

    String insertResponse =  "method was called";

    return insertResponse;
}

}

Whatever I have done I get:

Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions Transactionable is an interface, and JAXB can't handle interfaces.

Please do not redirect me to: http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html

Later Edit:

I am using jersey implementation.

You need to use some java-bean class for the parameter type - otherwise, JAX-RS won't be able to create and populate the parameter for you method before invoking it.

EDIT: Quoting from https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2145 :

In general the Java type of the method parameter may:

Be a primitive type;

Have a constructor that accepts a single String argument;

Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String) and java.util.UUID.fromString(String));

Have a registered implementation of javax.ws.rs.ext.ParamConverterProvider JAX-RS extension SPI that returns a javax.ws.rs.ext.ParamConverter instance capable of a "from string" conversion for the type. or

Be List, Set or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM