-1

I am generating sources from WSDL. It generate wrapper types. I want JAXB to generate primitive types instead of wrappers.

In pom.xml

                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.5.0</version>
                <executions>
                    <execution>
                        <id>generate-stubs</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <sourceType>wsdl</sourceType>
                            <sources>
                                <source>
                                    ${project.basedir}/src/main/resources/wsdl/my.wsdl
                                </source>
                            </sources>
                            <clearOutputDir>true</clearOutputDir>
                        </configuration>
                    </execution> 
</plugin>

In my WSDL file

<xs:element minOccurs="0" name="initBal" type="xs:long"/>

In generated class

protected Long initBal;

I tried to set a bindingDirectory to force to use primitive instead of wrappers. But seems that option is not available with jaxb2-maven-plugin 2.5.0 version.

<configuration>
                            <sourceType>wsdl</sourceType>
                            <sources>
                                <source>
                                    ${project.basedir}/src/main/resources/wsdl/my.wsdl
                                </source>
                            </sources>
                            <clearOutputDir>true</clearOutputDir>
                            <xjbSources>
                                <xjbSource>${project.basedir}/src/main/resources/wsdl/aBindingConfiguration.xjb</xjbSource>
                            </xjbSources>
                        </configuration>

aBindingConfiguration.xjb file

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
    <jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
        <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
    </jaxws:bindings>
</jaxb:bindings>
era
  • 391
  • 4
  • 24
  • Look at https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.5.0/xjc-mojo.html for available configuration. And bindingDirectory is not listed. – Michael Katt Jul 18 '21 at 13:41
  • bindingDirectory seems not available in 2.x alternative options is not clear – era Jul 18 '21 at 14:17
  • What are your intention using bindingDirectory? – Michael Katt Jul 18 '21 at 15:43
  • I want JAXB to generate primitive types instead of wrappers. – era Jul 18 '21 at 18:18
  • 1
    I assume you did not search Stackoverflow previously for your question. Because I found your answer in another Stackoverflow question. https://stackoverflow.com/questions/13801401/xml-schema-type-that-generates-java-primitive-type-using-jaxb-doesnt-add-requir by Looking for jaxb generate primitives. You have to remove minOccurs="0" which indicates a nullable value. – Michael Katt Jul 18 '21 at 18:36
  • I can't change the WSDL. I update the question with xjb config. But still JAXB generate wrapper types. – era Jul 18 '21 at 18:44
  • 1
    You can not use primitive type for nullable value because primitives are not nullable. – Michael Katt Jul 18 '21 at 21:23
  • As Katt indicted by setting minOccurs="0" JAXB generate primitive type. By setting default="0" in WSDL possible to set a default value for wrapper. – era Jul 19 '21 at 06:19

1 Answers1

1

Normally you can not use primitive type for nullable value because primitives are not nullable. You have to remove minOccurs="0" which indicates a nullable value.

look at XML Schema type that generates Java primitive type using JAXB doesn't add required to it

But looking further I found a setting for JAXB bindings which is not described for the official Customize JAXB Bindings https://docs.oracle.com/javase/tutorial/jaxb/intro/custom.html

optionalProperty="primitive"

look at https://download.oracle.com/javaee-archive/jaxb.java.net/users/2011/05/10121.html and JAXB compiler is binding xs:boolean to Java Boolean wrapper class, instead of boolean primitive type

Also you could try using JAXB binding for additional methods to ask for value state

 generateIsSetMethod = "true"
Michael Katt
  • 615
  • 1
  • 5
  • 18