Hello,
I am an Apache CXF newbie, with limited experience developing REST APIs and
also limited experience with JAXB and JAX-RS. I am having an issue using a
Class (that was generated using jaxb2:xjc) as a parameter in a REST API
method signature.
My apologies in advance for getting into some JAXB related details. The
problem I describe might seem more JAXB related at first, but I think that
in the end, it is something that I would have better luck getting answered
here. Hopefully someone has run into this issue and knows the proper
solution.
My REST API method signature looks something like this:
@POST
@Consumes({"application/json","application/xml"})
public Response saveFoo(Foo) {
.
.
.
}
The class that is generated from xjc looks something like this:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Foo", propOrder = { "bar" })
public class Foo {
protected String bar;
.
.
.
}
When I use this class, I get the following error:
JAXBException occurred : unexpected element (uri:"", local:"foo"). Expected
elements are <{http://acme.com/model/}foo>,........
After some investigating, I found out that the problem goes away whenever I
add the @XmlRootElement annotation to the class. I tested this by copying
the generated class into a different package and manually adding the
annotation. Of course, I would like it to be generated by xjc.
I discovered that the @XmlRootElement wasn't getting generated because I was
defining the XSD for the element like this:
<xs:complexType name = "Foo">
<xs:sequence>
<xs:element name="bar" type="xs:string" />
</xs:sequence>
</xs:compexType>
<xs:element name="foo" type="tns:Foo">
I later found out that the @XmlRootElement gets generated if I define the
element like this:
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="bar" type="xs:string" />
</xs:sequence>
</xs:compexType>
</xs:element>
The generated class looks like this:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "bar" })
@XmlRootElement(name="Foo")
public class Foo {
protected String bar;
.
.
.
}
Here we see that the @XmlRootElement that I needed gets generated, but the
name attribute for the @XmlType annotation is blank. The XSD won't let me
specify the name in both the element and the complexType.
So when I attempt to use this generated class, I get a similar error.
So it seems that I am caught in the middle. I can't get all the proper
artifacts generated.
It looks like it is possible to write (or possibly use an existing) plugin
for the generator, and I can think of a few different ways I can do this
myself, but I would rather avoid doing something like when there is most
likely a simple solution to this issue. It seems like I should be able to
generate a class that I can use for this purpose.
I am more than glad to provide any missing information that someone would
need to see in order to know what the problem is.
--
View this message in context:
http://cxf.547215.n5.nabble.com/XMLRootElement-for-parameters-in-REST-method-signatures-tp4537057p4537057.html
Sent from the cxf-user mailing list archive at Nabble.com.