簡體   English   中英

javax.xml.ws.WebServiceException:javax.xml.bind.MarshalException-帶有鏈接的異常:

[英]javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException - with linked exception:

我正在開發一個肥皂網絡服務。 我將JPA與namedquery批注一起用於數據查詢。 但是在我的查詢中使用諸如sum之類的聚合函數與group by相結合會導致一些異常。 我的代碼如下所示。

@Entity
@Cacheable(false)
@Table(name = "criteriatable")
@XmlRootElement
@NamedQueries(
{
@NamedQuery(name = "Criteriatable.findAll", query = "SELECT c FROM Criteriatable c"),
@NamedQuery(name = "Criteriatable.findById", query = "SELECT c FROM Criteriatable c WHERE c.id =    :id"),
@NamedQuery(name = "Criteriatable.findByCriteriaid", query = "SELECT c FROM Criteriatable c WHERE c.criteriaid = :criteriaid"),
@NamedQuery(name = "Criteriatable.findBySubcriteriaid", query = "SELECT c FROM Criteriatable c WHERE c.subcriteriaid = :subcriteriaid"),
@NamedQuery(name = "Criteriatable.findByProjectid", query = "SELECT c FROM Criteriatable c WHERE c.projectid = :projectid"),
@NamedQuery(name = "Criteriatable.findAllCriteriatableGroup", query = "SELECT c.criteriagroupname AS groupname, SUM(c.weight) AS groupweight  FROM Criteriatable c WHERE c.projectid = :projectid GROUP BY c.criteriagroupname"),
@NamedQuery(name = "Criteriatable.SumAllWeightByProjectid", query = "SELECT SUM(c.weight) AS TotalProjectWeight FROM Criteriatable c WHERE c.projectid = :projectid")    
})
  public class Criteriatable implements Serializable
{
@Basic(optional = false)
@NotNull
@Column(name = "weight")
private double weight;
@Basic(optional = false)
@NotNull
@Column(name = "status")
private int status;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "criteriagroupname")

WS方法

@WebMethod(operationName = "getAllCriteriaGroupByProjectid")
public List<Criteriatable> getAllCriteriaGroupByProjectid(@WebParam(name = "projectid") int projectid)
{
    EntityManager em = emf.createEntityManager();
    String find = "Criteriatable.findAllCriteriatableGroup";
    TypedQuery<Criteriatable> query = em.createNamedQuery(find, Criteriatable.class);
    query.setParameter("projectid", projectid);
    return query.getResultList();
}

我收到的錯誤是:

SEVERE:   Error occured
javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class [Ljava.lang.Object; nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.]

請我需要幫助。 謝謝。

我認為問題是您從網絡方法返回了List 嘗試返回自己的包裝器類,該包裝器類會將其中的列表作為字段。 像這樣

public class CriteriatablesWrapper {
    private List<Criteriatable> criteriatableList;
    // getter and setter
}

並將您的服務方法簽名更改為

@WebMethod(operationName = "getAllCriteriaGroupByProjectid")
public CriteriatablesWrapper getAllCriteriaGroupByProjectid(@WebParam(name = "projectid") int projectid) {
    ...
}

您的查詢Criteriatable.findAllCriteriatableGroup沒有返回Criteriatable對象。

它返回[groupName,groupweight]元組的列表。

請記住,泛型類型信息在運行時不可用,因此您的List<Criteriatable>集合在運行時將是List<Object[]> (實際上是List<Object[2]> ,但是您不能那樣寫)。

隨后,這將使JAXB映射失敗。

您需要為結果定義一個POJO:

@XmlRootElement
public class GroupTotal {

    private String groupName;

    private double groupTotal;

    public GroupTotal(String groupName, double groupTotal) {
         this.groupName = groupName;
         this.groupTotal = groupTotal;
    }

    // getters and setters ...
}

修改您的查詢:

SELECT NEW your.package.GroupTotal (c.criteriagroupname, SUM(c.weight))
  FROM Criteriatable c WHERE c.projectid = :projectid GROUP BY c.criteriagroupname

最后執行:

TypedQuery<GroupTotal> query = em.createNamedQuery(find, GroupTotal.class);

我沒有嘗試過,但是它應該使您指向正確的方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM