JAXB generating JAXBElement<String> instead of String
Solution 1
What I had to do is to wrap jaxb:globalBindings
with another jaxb:bindings
.
<jaxb:bindings version="2.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings>
<jaxb:globalBindings generateElementProperty="false"/>
</jaxb:bindings>
</jaxb:bindings>
Now everything is working, there is no JAXBElement<String>
generated anymore.
Solution 2
You can't have nillable and minoccurs together. Remove the minoccurs as it doesn't make sense for strings anyway.
Solution 3
I think you want to add in your jaxb-binding.xml:
<jaxb:bindings ... xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jaxb:globalBindings generateElementProperty="false">
<xjc:simple />
<!-- ... -->
</jaxb:globalBindings>
</jaxb:bindings>
Paulius Matulionis
Check some of my posts at: http://pauliusmatulionis.blogspot.co.uk/
Updated on February 10, 2022Comments
-
Paulius Matulionis over 2 years
I am using
Apache CXF
cxf-codegen-plugin
Maven
plugin to generate sources fromWSDL
file. Problem is that I getJAXBElement<String>
generated instead ofString
. I have added thejaxb-bindings.xml
file which looks like this:<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"> <jaxb:globalBindings generateElementProperty="false"/> </jaxb:bindings>
This should prevent
JAXB
to generateJAXBElement<String>
. But it is not working I still haveJAXBElement<String>
generated instead ofString
.My
Maven
plugin looks like this:<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.runtime.version}</version> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>${cxf.runtime.version}</version> </dependency> </dependencies> <executions> <execution> <id>generate-jaxb</id> <phase>generate-sources</phase> <configuration> <additionalJvmArgs>-Dfile.encoding=UTF8</additionalJvmArgs> <wsdlOptions> <wsdlOption> <wsdl>src/main/resources/wsdl/Cubiks.wsdl</wsdl> <extraargs> <extraarg>-b</extraarg> <extraarg>${basedir}/jaxb-bindings.xml</extraarg> <extraarg>-b</extraarg> <extraarg>${basedir}/jaxws-bindings.xml</extraarg> <extraarg>-exsh</extraarg> <extraarg>true</extraarg> <extraarg>-wsdlLocation</extraarg> <extraarg></extraarg> </extraargs> </wsdlOption> <wsdlOption> <wsdl>src/main/resources/wsdl/CubiksCallBackService.wsdl</wsdl> <extraargs> <extraarg>-b</extraarg> <extraarg>${basedir}/jaxws-bindings.xml</extraarg> <extraarg>-b</extraarg> <extraarg>${basedir}/jaxb-bindings.xml</extraarg> <extraarg>-exsh</extraarg> <extraarg>true</extraarg> <extraarg>-p</extraarg> <extraarg>com.cubiks.ws.callback</extraarg> <extraarg>-wsdlLocation</extraarg> <extraarg></extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin>
CXF
version is2.6.0
. Does someone know where might be the problem?EDIT
The XSD is very huge. This is the element which generating
JAXBElement<String>
<xs:complexType name="ServiceResponse"> <xs:sequence> <xs:element minOccurs="0" name="RequestStatus" type="tns:RequestStatus"/> <xs:element minOccurs="0" name="RequestStatusDescription" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:element name="ServiceResponse" nillable="true" type="tns:ServiceResponse"/>
And the generated source is:
@XmlElementRef(name = "RequestStatusDescription", namespace = "http://www.cubiksonline.com/2009/08/AssessmentProvider", type = JAXBElement.class) protected JAXBElement<String> requestStatusDescription;
-
Paulius Matulionis almost 12 yearsI am trying to add it but I am getting: com.sun.istack.SAXParseException2: The "xjc:simple" customization is not associated with any schema element.
-
Paulius Matulionis almost 12 yearsI have managed to <xjc:simple /> correctly but still the same. JAXBElement<String> is generated.
-
Paulius Matulionis almost 12 yearsAnd why is that I can't have them together? By the way the wsdl file is provided by the client, we didn't create it. I understand that they does not make sense, but it its not for me to decide to remove them or not.
-
Frank Pavageau almost 12 yearsRight, I've now checked out a WS to test, it's the
generateElementProperty="false"
attribute which avoids theJAXBElement
wrapper in my case. Sorry for the noise. -
Ian Roberts almost 12 yearsIf the schema has both then you need a JAXBElement to be able to distinguish between the cases where the XML element is missing (allowed by minOccurs) and where it is present but
xsi:nil
. If the schema only allowed one or the other then you wouldn't need the JAXBElement as anull
property value would be enough. -
Ian Roberts almost 12 yearsThis post explains it nicely.
-
ramsinb almost 12 yearsWell what does it mean for a string to be nillable and have minoccurs=0? What would the XML look like if those condition were met? Anyway have a look at here to make sure you are using the binding correctly there is an example Showing how to configure the maven plugin. Your binding file is correct though I suspect if you configure your maven plugin correctly it will work. Since you can't change the wsdl this is your only option.
-
Paulius Matulionis almost 12 yearsThat's odd, I wrapped my globalBindings with jaxb:bindings and it worked. <jaxb:bindings version="2.0" xmlns:jaxb="java.sun.com/xml/ns/jaxb"> <jaxb:bindings> <jaxb:globalBindings generateElementProperty="false"/> </jaxb:bindings> </jaxb:bindings> I still can't understand why.
-
webuster over 8 yearsHappy to report this solution works in JDeveloper as well. Just create an xml file then pass it in the wizard at the phase when you specify the URL/location of the WSDL.
-
Darrel K. over 6 yearsThis worked in NetBeans 8.2 as well. Right click on the service and "edit web service attributes". Then go to "WSDL customization" tab and at the bottom under "External Binding Files" add this file and regenerate. All Strings resolved as String. Tanks!
-
Reema over 2 yearsWhere do you add this snippet ? @PauliusMatulionis