user640118
user640118

Reputation: 843

Using a JAXB annotated class as input parameter for a JAX-WS web service

I'm in the early stages of developing a web service to process a Customer provided as input to the service using JAX-WS. Netbeans 7.0, Glassfish 3.1 with the included Metro reference implementation.

I've got a very simple version working with the below code. This generates the wsdl correctly, deploys, and I can use SoapUI to consume the wsdl and test the web service.

@WebService(serviceName = "CustomerBroker")
public class CustomerBroker {

private static final String RTN_MSG_CREATE = "Create";
private static final String RTN_MSG_UPDATE = "Update";

/** Process the supplied Customer */
@WebMethod(operationName = "processCustomer")
public String processCustomer(@WebParam(name = "customerId") String customerId, @WebParam(name = "customerName") String customerName) {

    String returnMsg = "";

    if (StringUtils.isBlank(customerId)) {
        returnMsg = RTN_MSG_CREATE;
    } else {
        returnMsg = RTN_MSG_UPDATE;
    }

    return returnMsg;
    }
}

Instead of individual string parameters for every Customer data field, I'd like the parameter to the method to be a JAXB annotated class. I've developed the below class. I've used JAXB sucessfully in the past with a JAX-RS web service and used that code as a guide.

@XmlType(name="Customer", propOrder={"customerId", "customerName"})
@XmlRootElement(name="Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

@XmlElement(name="customerId")
public String customerId;

@XmlElement(name="customerName")
public String customerName;

}

When I try to use this class as a parameter to the web service method I run into errors. I've tried numerous updates to the web service annotations, with the last try as follows:

public String processCustomer(@WebParam(name = "Customer") Customer c)

The error I get on deployment is: java.lang.RuntimeException: Servlet web service endpoint '' failure.

I'd like the wsdl generated and the service to operate with the JAXB Customer class as the input. Should I be able to accomplish this with the correct annotations, or is there something else I'm missing? I've found plenty on JAXB and JAX-WS, but nothing that matches this simple exmaple. Thanks.

Full Stack Trace:

Error occurred during deployment: Exception while loading the app :          java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.RuntimeException: Servlet web service endpoint '' failure. Please see server.log for more details.
C:\NetBeansProjects\CustomerBroker\nbproject\build-impl.xml:749: 
The module has not been deployed.
at    org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment.deploy(Deployment.java:187)
at org.netbeans.modules.j2ee.ant.Deploy.execute(Deploy.java:106)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor66.invoke(Unknown Source)
at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at     org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:284)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:539)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:154)

Upvotes: 0

Views: 8987

Answers (3)

titojusto
titojusto

Reputation: 11

I had a similar trouble... I needed an hyphen in a object in a WS, but the object is inner to another object...so, my solution was...

Hint: I needed a empty constructor...

@XmlType(name="Entrada")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entrada {

@XmlElement(name="tipo-tx")
String tipo_tx;

@XmlElement(name="item")
ArrayList<Item> item;

public Entrada() {}


public Entrada(String tipo_tx, ArrayList<Item> item) {
    this.tipo_tx = tipo_tx;
    this.item = item;       
}
....
//getter & setters...
}

and in my ws caller...

@WebMethod(operationName = "WSMC")
@WebResult(name="item")
public ArrayList<Item> WSMC(@WebParam(name="entrada") Entrada entrada) {
    logger.info("entrada: " + entrada);
    logger.info("Tipo TX: " + entrada.getTipo_tx());
}

I hope this can help to you

Upvotes: 0

user640118
user640118

Reputation: 843

I think it's an issue with Glassfish 3.1. I ran it with Glassfish 3.0 and it worked just fine (after removing the soap 1.2 binding annotation).

Upvotes: 1

have you tried using the @XmlType annotation in the Customer class?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType (name="Customer",
          propOrder={"customerId", "customerName"})

Upvotes: 0

Related Questions