简体   繁体   中英

JAXB annotations on a Quarkus Panache entity causes REST call to fail

I have annotated a class to be a Panache Entity. However, I have also included JAXB annotations:

@Entity
@XmlRootElement(name = "Person")
@XmlAccessorType(XmlAccessType.NONE)
public class Person extends PanacheEntity {

    @XmlAttribute(name = "Name")
    public String name;
}

When I try to return the object from a REST call I get the following exception:

2019-12-08 08:30:01,917 ERROR [org.jbo.res.res.i18n] (vert.x-worker-thread-3) RESTEASY002005: Failed executing GET /person: org.jboss.resteasy.plugins.providers.jaxb.JAXBMarshalException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
model.Person#name has mutually exclusive annotations @javax.xml.bind.annotation.XmlTransient and @javax.xml.bind.annotation.XmlAttribute
    this problem is related to the following location:
        at @javax.xml.bind.annotation.XmlTransient()
        at model.Person
    this problem is related to the following location:
        at @javax.xml.bind.annotation.XmlAttribute(namespace="##default", name="Name", required=false)
        at model.Person

    at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:187)
    at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.writeTo(AbstractJAXBProvider.java:149)

So it seems the the Quarkus Panache framework is adding @XmlTransient to my public property.

I can get round this by changing the access of the name property to private and including getters/setters. However, this approach loses one of the benefits of Panache that your code is more compact and readible.

Is there anyway of keeping the public accessor of the class property and still making it work with JAXB?

Interesting use case.

I think we need to check that the attributes don't have any conflicting JAXB annotation before adding the @XmlTransient one automatically.

And it's probably also an issue with JSON-B/Jackson as we do the same thing.

I don't see any obvious workaround: we need to fix it in Quarkus.

Could you open a GitHub issue with a simple reproducer? Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM