Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-WS custom object received in request is null

I've used JAX-WS before, but have not passed a custom object as a parameter before. I'm using GlassFish 3.1, NetBeans 7.3 and created services through NetBeans JAX-WS wizard. My problem is when custom object (Criteria) passed to service is received as null on the server. I can pass default types like int successfully.

@WebService(serviceName = "ECLService")
@Stateless()
public class ECLService {

    @EJB
    PersistenceImpl persistence;
    @WebMethod(operationName = "listRevisions")
    public List<Revision> listRevisions(@WebParam(name="criteria")Criteria criteria) {
        System.out.println("criteria is "+(criteria ==null ? "null":" not null"));
        List<Revision> revisions = persistence.getRevisions(criteria);

        return revisions;
    }
}

Criteria.java

@XmlRootElement
public class Criteria implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String LIST_TYPE = "criteria.key.listtype";

    public static final String TYPE_ALL = "criteria.value.all";
    public static final String TYPE_ERROR = "criteria.value.error";
    public static final String TYPE_ARCHIVE = "criteria.value.archive";
    public static final String TYPE_APPROVAL = "criteria.value.approval";

    private Map<String, String> parameters;

    public Map<String, String> getParameters() {
        return parameters;
    }

    public String getParameter(String key) {
        if (parameters==null || key==null) {
            return null;
        } else {
            return parameters.get(key);
        }
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters = parameters;
    }

    public void setParameter(String key, String value) {        
        if (parameters==null) {
            parameters = new HashMap<String,String>();
        }        
        parameters.put(key, value);
    }

    public void setType(String type) {
        setParameter(LIST_TYPE, type);
    }

    public String getType() {
        return getParameter(LIST_TYPE);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 43 * hash + (this.parameters != null ? this.parameters.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Criteria other = (Criteria) obj;
        if (this.parameters != other.parameters && (this.parameters == null || !this.parameters.equals(other.parameters))) {
            return false;
        }
        return true;
    }
}

Is there anything I'm missing like annotation or something?

Message sent looked like this:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:listRevisions xmlns:ns2="http://webservice.ecl.abc.com/"><ns2:criteria>
<type>TYPE_ALL</type></ns2:criteria></ns2:listRevisions></S:Body></S:Envelope>

HTTP/1.1 200 OK
server: grizzly/1.9.50
Content-Type: text/xml;charset=utf-8
 Transfer-Encoding: chunked
Date: Fri, 17 May 2013 07:37:15 GMT
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body>
<ns2:listRevisionsResponse xmlns:ns2="http://webservice.ecl.abc.com/"/></S:Body> 

</S:Envelope>
like image 448
user2361862 Avatar asked Oct 04 '22 03:10

user2361862


2 Answers

Try adding targetNamespace to WebParam annotation, with the namespace of the schema.

I faced the same issue. The root element was defaulted to the service namespace instead of schema namespace. I added targetNamespace to WebParam and that helped resolve this issue.

@WebResult(name="status")
public String send(@WebParam(name="event",targetNamespace="http://efw/event/schema") EventType event);
like image 120
sara Avatar answered Oct 22 '22 23:10

sara


  1. getParameter/setParameter methods does not follow JavaBean convension. If you don't want not follow this convension use @XmlAccessorType(XmlAccessType.FIELD) at the class level - JAXB would look at fields, not methods.
  2. getParameter/setParameters work with Map type, it can be done, but with some additional work (JAXB java.util.Map binding)
  3. getType/setType are the only "proper" methods pair, so they are properly treated by JAXB
like image 1
Piotr Kochański Avatar answered Oct 22 '22 23:10

Piotr Kochański