【问题标题】:JAX-WS custom object received in request is null请求中收到的 JAX-WS 自定义对象为空
【发布时间】:2013-05-11 08:08:04
【问题描述】:

我之前使用过 JAX-WS,但之前没有将自定义对象作为参数传递。我正在使用 GlassFish 3.1、NetBeans 7.3 并通过 NetBeans JAX-WS 向导创建服务。 我的问题是当传递给服务的自定义对象(标准)在服务器上被接收为 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;
    }
}

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;
    }
}

有什么我缺少的东西,比如注释之类的吗?

发送的消息如下所示:

<?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>

【问题讨论】:

  • 您发送的消息是什么样的?
  • @BlaiseDoughan:我已经编辑了帖子以添加发送的消息。谢谢。
  • 请问您解决了吗?

标签: jaxb jax-ws jax-ws-customization


【解决方案1】:

尝试使用架构的命名空间将 targetNamespace 添加到 WebParam 注释。

我遇到了同样的问题。根元素默认为服务命名空间而不是模式命名空间。我将 targetNamespace 添加到 WebParam,这有助于解决此问题。

@WebResult(name="status")
public String send(@WebParam(name="event",targetNamespace="http://efw/event/schema") EventType event);

【讨论】:

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

    【讨论】:

      【解决方案3】:

      我还不能发表评论(声誉太低),但我解决了这个问题。我的解决方案是更改@WebParam 的名称,使其不是类的名称

      public List&lt;Revision&gt; listRevisions(@WebParam(name="crit")Criteria criteria) {

      而不是

      public List&lt;Revision&gt; listRevisions(@WebParam(name="criteria")Criteria criteria) {

      我不知道为什么会这样。也许有人可以提供见解。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2011-11-27
        • 1970-01-01
        • 1970-01-01
        • 2013-03-15
        相关资源
        最近更新 更多