不能使用moxy将持久化实体封送到xml表示中

时间:2014-02-10 01:32:12

标签: rest jaxb glassfish entities moxy

我正在尝试使用RESTful Web服务将实体实例解组为XML树结构。最初我得到了一个重复的命名异常,因为MOXy无法解析实体的属性名称和字段名称。所以我将所有注释都移到了现场级别。现在当我再次运行程序时,我得到一个不受支持的属性异常

行李类:

@Entity
@NamedQuery(name = "findBagWithTagNo", query = "select b from Bag b where b.bagTagNo=:bagTagNo")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Bag implements Serializable {

    private static final long serialVersionUID = 1L;
    @XmlElement
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @XmlElement
    //@XmlID
    private String bagTagNo;

    // @XmlInverseReference(mappedBy="bags")

    @ManyToOne
    @XmlInverseReference(mappedBy = "bags")
    @XmlElement
    private Passenger passenger;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getBagTagNo() {
        return bagTagNo;
    }

    public void setBagTagNo(String bagTagNo) {
        this.bagTagNo = bagTagNo;
    }


    public Passenger getPassenger() {
        return passenger;
    }

    public void setPassenger(Passenger passenger) {
        this.passenger = passenger;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Bag)) {
            return false;
        }
        Bag other = (Bag) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
//        return "kharesoft.sita.Bag[ id=" + id + " ]";
        return bagTagNo;
    }
}

乘客等级:

@XmlRootElement
@XmlType(propOrder = {"id", "firstName", "lastName", "boardingNo", "seatNo", "flightNo", "dateOfFlight", "bags"})
@Entity
@NamedQuery(name = "findPaxByBagTag", query = "select p from Passenger p,IN (p.bags) b where b.bagTagNo=:bagTagNo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Passenger implements Serializable {

    @XmlElement
    private String firstName;
    @XmlElement
    private String lastName;
    @XmlElement
    private String boardingNo;
    @XmlElement
    private String seatNo;
    @XmlElement
    private String flightNo;
    @XmlElement
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateOfFlight;
    @XmlElementWrapper
    //@XmlIDREF
     //@XmlInverseReference(mappedBy="passenger")
    @XmlElements({
    @XmlElement(name = "bagtag")})
    @OneToMany(mappedBy = "passenger")
    private Set<Bag> bags;
    private static final long serialVersionUID = 1L;
    @XmlElement
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<Bag> getBags() {
        return bags;
    }

    public void setBags(Set<Bag> bags) {
        this.bags = bags;
    }

    public String getBoardingNo() {
        return boardingNo;
    }

    public void setBoardingNo(String boardingNo) {
        this.boardingNo = boardingNo;
    }

    public Date getDateOfFlight() {
        return dateOfFlight;
    }

    public void setDateOfFlight(Date dateOfFlight) {
        this.dateOfFlight = dateOfFlight;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFlightNo() {
        return flightNo;
    }

    public void setFlightNo(String flightNo) {
        this.flightNo = flightNo;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSeatNo() {
        return seatNo;
    }

    public void setSeatNo(String seatNo) {
        this.seatNo = seatNo;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Passenger)) {
            return false;
        }
        Passenger other = (Passenger) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "kharesoft.sita.Passenger[ id=" + id + " ]";
    }
}
javax.ws.rs.WebApplicationException: javax.xml.bind.PropertyException: Unsupported Property
    at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:183)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.xml.bind.PropertyException: Unsupported Property
    at org.eclipse.persistence.jaxb.JAXBMarshaller.getProperty(JAXBMarshaller.java:231)
    at com.sun.jersey.core.impl.provider.entity.XMLListElementProvider.writeList(XMLListElementProvider.java:137)
    at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:181)
    ... 33 more

1 个答案:

答案 0 :(得分:0)

以下是以前在泽西岛存在的错误。 Jersey过去只支持JAXB参考实现,这是在Jersey试图在MOXy上设置JAXB参考实现特定属性时引起的。

javax.ws.rs.WebApplicationException: javax.xml.bind.PropertyException: Unsupported Property
    at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:183)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)

解决方案

  • 升级到较新版本的Jersey
  • 升级到支持RI的该属性的新版MOXy
  • 通过MessageBodyReader / MessaggeBodyWriter改为使用MOXy。