编程语言
首页 > 编程语言> > java – 有没有办法配置JAXB插件为boolean getter方法而不是“is”追加“get”

java – 有没有办法配置JAXB插件为boolean getter方法而不是“is”追加“get”

作者:互联网

我在我的项目中使用了下面提到的JAXB插件

<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<version>1.1.1</version>

其中为boolean元素添加“get”.但是在迁移到新插件时

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>

我得到布尔类型元素的getter方法的“is”.但代码期望旧的签名.例如

假设,我们有以下boolean类型的元素.

a)fileCreated.

New Plugin在Generated列下生成了以下方法签名.我在Expected列下列出了预期的方法.

       Generated                           Expected
  boolean isFileCreated()             boolean getFileCreated()

我们的团队没有维护一些代码片段,因此更改调用代码不在我们手中.请建议是否有办法配置此插件,以便它像我们期望的那样为boolean生成getter.

提前致谢.

这是pom.xml中的JAXB插件配置

<plugin>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.12.3</version>

                    <executions>
                        <execution>
                            <id>kyc</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>

                            <!-- Added for generating getter for boolean element in XSDs with prefix "get"    starts-->
                            <enableIntrospection>true</enableIntrospection>

                            <!-- Added for generating getter for boolean element in XSDs with prefix "get"    ends-->



                                <generatePackage>XXX.XXX.APackage</generatePackage>
                                <schemaDirectory>src/main/resources/XXX/</schemaDirectory>
                                <generateDirectory>${project.build.directory}/generated-sources/XXX/generateDirectory>
                                <verbose>true</verbose>
                            </configuration>
                        </execution>
    </executions>
    </plugin>

进行此更改后,我仍然会获得类型为boolean的“is”附加属性名称.

解决方法:

您可以在maven插件中使用enableIntrospection选项. See Here

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.3</version>
    <executions>

        <execution>
            <id>xjc1</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Xannotate</arg>
                    <arg>-nv</arg>
                    <arg>-Xnamespace-prefix</arg>
                </args>
                <extension>true</extension>
                <schemas>
                    <schema>
                        <fileset>
                            <directory>${basedir}/src/main/resources/schema</directory>
                            <includes>
                                <include>*.xsd</include>
                            </includes>
                        </fileset>
                    </schema>
                </schemas>

                <enableIntrospection>true</enableIntrospection>

                <debug>true</debug>
                <verbose>true</verbose>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.0</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>0.6.0</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-namespace-prefix</artifactId>
                        <version>1.1</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>

使用元素< xs:element minOccurs =“0”name =“flag”type =“xs:boolean”/>

<enableIntrospection>false</enableIntrospection>

生成的类public Boolean isFlag(){

代替

<enableIntrospection>true</enableIntrospection>

生成的类public Boolean getFlag(){

标签:java,maven-3,jaxb,maven-jaxb2-plugin
来源: https://codeday.me/bug/20190627/1310073.html