How to generate pojo from json schema by maven

本文介绍了如何在线生成JSON Schema以及如何通过Maven配置将JSON Schema转换为Java POJO。首先,可以利用jsonschema.net网站创建JSON Schema。然后,在pom.xml中添加特定的Maven插件配置,并确保JSON Schema文件位于${basedir}/src/main/resources/schema目录下。执行Maven命令后,将在项目中自动生成POJO。更多信息可参考jsonschema2pojo GitHub仓库。

Generate json schema from json

you can generate json schema online through the following website.

http://jsonschema.net/

Generate pojo by maven

Add the following maven configuration to your pom.xml, and execute maven command. you will find pojo generated by maven in your project.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>

<dependencies>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.0.0</version>
        </dependency>
</dependencies>

<build>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <defaultGoal>install</defaultGoal>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>0.4.20</version>
                <configuration>
                    <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
                    <targetPackage>com.example.types</targetPackage>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>

Note: you need put your json schema in path ${basedir}/src/main/resources/schema. 
you can visit website https://github.com/joelittlejohn/jsonschema2pojo for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值