请求中收到的JAX-WS自定义对象为null

use*_*862 5 jax-ws jaxb jax-ws-customization

我之前使用过JAX-WS,但之前没有将自定义对象作为参数传递过.我正在使用GlassFish 3.1,NetBeans 7.3并通过NetBeans JAX-WS向导创建服务.我的问题是当服务器上接收到传递给服务的自定义对象(Criteria)为null时.我可以成功传递像int这样的默认类型.

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有像注释那样的东西?

发送的消息看起来像这样:

<?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>
Run Code Online (Sandbox Code Playgroud)

Pio*_*ski 1

  1. getParameter/setParameter 方法不遵循 JavaBean 约定。如果您不想遵循此约定,请@XmlAccessorType(XmlAccessType.FIELD)在类级别使用 - JAXB 将查看字段,而不是方法。
  2. getParameter/setParameters 使用 Map 类型,可以完成,但需要一些额外的工作(JAXB java.util.Map 绑定
  3. getType/setType 是唯一“正确”的方法对,因此 JAXB 会正确处理它们