gpt4 book ai didi

java - 如何使用 CXF xjcplugin 生成 equals 和 hashCode

转载 作者:行者123 更新时间:2023-11-30 11:30:15 25 4
gpt4 key购买 nike

我正在尝试从 XSD 文件生成一个包含方法 toString、equals 和 hashCode 的 Java 类。我开始使用 toString,但我无法弄清楚如何让 CXF 插件生成 equals 和 hashCode 方法。

这是我的 XSD 文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/messagedefinitions"
xmlns="http://www.example.com/messagedefinitions">


<xs:element name="Message">
<xs:complexType>
<xs:sequence>
<xs:element name="status" type="Status"/>
<xs:element name="id" type="Identifier"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleType name="Status">
<xs:restriction base="xs:string">
<xs:maxLength value="4"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="Identifier">
<xs:restriction base="xs:int"/>
</xs:simpleType>

</xs:schema>

我的工作 POM(不工作的被注释掉了):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>xsd-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>xsd-demo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>2.6.1</cxf.version>
<cxf.xjc.plugin.version>2.6.1</cxf.xjc.plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-common</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>xjc-utils</artifactId>
<type>pom</type>
<version>${cxf.xjc.plugin.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>cxf-xjc-runtime</artifactId>
<version>${cxf.xjc.plugin.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>${cxf.xjc.plugin.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>

<configuration>
<sourceRoot>${basedir}/target/generated-sources</sourceRoot>
<xsdOptions>
<xsdOption>
<extension>true</extension>
<xsd>${basedir}/src/main/resources/xsd/Message.xsd</xsd>
<bindingFile>${basedir}/src/main/resources/bindings.xml</bindingFile>
<extensionArgs>
<arg>-Xts:style:multiline</arg>
<!--<arg>-Xequals</arg>-->
<!--<arg>-XhashCode</arg>-->
</extensionArgs>
</xsdOption>
</xsdOptions>
<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:${cxf.xjc.plugin.version}</extension>
<!--<extension>org.apache.cxf.xjcplugins:cxf-xjc-XhashCode:${cxf.xjc.plugin.version}-->
<!--</extension>-->
<!--<extension>org.apache.cxf.xjcplugins:cxf-xjc-Xequals:${cxf.xjc.plugin.version}-->
<!--</extension>-->
</extensions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

绑定(bind)文件:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:version="2.1">
<jaxb:globalBindings>
<xjc:serializable uid="1234"/>
</jaxb:globalBindings>
</jaxb:bindings>

如果我取消注释 Xequals 的注释行,我会收到此错误:

[错误] 无法在项目 xsd-demo 上执行目标 org.apache.cxf:cxf-xjc-plugin:2.6.1:xsdtojava (generate-sources):无法下载扩展工件:请求的下载不存在。在中央 ( http://repo1.maven.org/maven2 ) 找不到工件 org.apache.cxf.xjcplugins:cxf-xjc-Xequals:jar:2.6.1

很好,我意识到工件不存在,但我不知道如何配置 CXF 插件来生成 equals 和 hashCode 方法。你知道我如何配置 CXF 插件来生成 equals 和 hashCode 方法吗?在此先感谢您的帮助。

最佳答案

我无法让 cxf-xjc-plugin 从 XML 模式文件生成 equals 和 hashCode 方法。我放弃了 cxf-xjc-plugin,转而使用 maven-jaxb2-plugin。这是我使用的插件配置:

<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>generate</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<extension>true</extension>
<generatePackage>my.package.name</generatePackage>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
</args>
<schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
<schemaIncludes>
<include>xsd/*.xsd</include>
</schemaIncludes>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>

关于java - 如何使用 CXF xjcplugin 生成 equals 和 hashCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17876332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com