Jackson 根据 xsd 生成 Java

该博客介绍了如何利用Jackson根据XML Schema (xsd) 文件自动生成对应的Java类,从而避免手动编写。通过在Maven的pom.xml中配置特定的build插件,可以实现这一自动化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

– Start

点击此处观看本系列配套视频。


xsd 是定义 XML 格式的元数据,如果有 xsd,我们可以根据 xsd 生成 java,而不用我们手动定义相对应的 Java 类。我们只需要在 pom 中添加一个build 插件。

<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>shangbo</groupId>
  <artifactId>jackson</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>jackson</name>
  <url>http://maven.apache.org</url>

  <build>
    <plugins>
		<plugin>
		    <groupId>org.apache.cxf</groupId>
		    <artifactId>cxf-xjc-plugin</artifactId>
		    <version>3.2.3</version>
		    <configuration>
		        <extensions>
		            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:3.2.3</extension>
		        </extensions>
		    </configuration>
		    <executions>
		        <execution>
		            <id>generate-sources</id>
		            <phase>generate-sources</phase>
		            <goals>
		                <goal>xsdtojava</goal>
		            </goals>
		            <configuration>
		                <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
		                <xsdOptions>
		                    <xsdOption>
		                        <xsd>src/main/resources/schemas/Person.xsd</xsd>
		                        <packagename>shangbo.jackson.demo22</packagename>
		                    </xsdOption>
		                </xsdOptions>
		            </configuration>
		        </execution>
		    </executions>
		</plugin>
    </plugins>
  </build>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-core</artifactId>
	    <version>2.9.9</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-annotations</artifactId>
	    <version>2.9.9</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-databind</artifactId>
	    <version>2.9.9</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-lang3</artifactId>
	    <version>3.9</version>
	</dependency>
  </dependencies>
</project>

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

	<xs:element name="Person">
	    <xs:complexType>
	      <xs:sequence>
				<xs:element name="firstname" type="xs:string"/>
				<xs:element name="lastname" type="xs:string"/>
	      </xs:sequence>
	    </xs:complexType>
	</xs:element>

</xs:schema>

– 更多参见:Jackson 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-05-27
– Written by ShangBo on 2019-05-27
– End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值