美文网首页
Json Schema 及 JsonSchema2pojo ma

Json Schema 及 JsonSchema2pojo ma

作者: coderrrrrrrrr | 来源:发表于2020-04-02 17:32 被阅读0次

Json Schema

Json Schema是一种json定义格式,允许你自己定义json的注释和验证json文本。

Json Schema

  • 描述现有数据格式。
  • 干净的人类和机器可读的文档。
  • 完整的结构验证,有利于自动化测试。
  • 完整的结构验证,可用于验证客户端提交的数据。

Json schema 格式

参照 https://blog.csdn.net/silence_xiao/article/details/81303935

官网 http://json-schema.org/understanding-json-schema/basics.html

JsonSchema2pojo

使用Json Schema生成POJO(Plain Ordinary Java Object 简单的Java对象)的插件。

The Maven plugin

配置方法:

  • 1.配置pom.xml引入jsonschema2pojo-maven-plugin并设置goal为generate.
        <plugins>
            <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>1.0.0</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>
  • 2.设置编译环境为Java 6及以上
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
  • 3.设置依赖包,可以选用Jackson、FastJson或者Gson。
        <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.5.4</version>
        </dependency>
  • 4.以上配置,即可在编译时,自动把${basedir}/src/main/resources/schema目录下定义的Json Schema生成相关的Class文件到com.example.types包下。

Maven plugin其他配置

  • 1.输出java文件到指定目录<outputDirectory>...</outputDirectory>.
            <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>0.4.37</version>
                <configuration>
                    <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
                    <targetPackage>cn.csg.common.vo</targetPackage>
                    <outputDirectory>${basedir}/src/main/java</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

这个配置可以直接生成java文件,在Idea环境编码时,其他项目无需在引用lib指向此项目的classes文件夹,只需要配置对此项目的依赖。

  • 2.为POJO生成builder方法<generateBuilders>true</generateBuilders>
        <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>0.4.37</version>
                <configuration>
                    <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
                    <targetPackage>cn.csg.common.vo</targetPackage>
                    <generateBuilders>true</generateBuilders>
                    <outputDirectory>${basedir}/src/main/java</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

设置后,生成builder。这个builder可以简单的在一行初始化一个对象:

MyObject o = new MyObject().withFoo("foo").withBar("bar").withBaz("baz");
  • 3.一个陷阱.
    不要设置此参数<sourceType>json</sourceType>!

其他

关于Schema定义其他特性 https://github.com/joelittlejohn/jsonschema2pojo/wiki/Reference

关于JsonSchema2pojo除Maven的其他插件和使用方法 https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started#the-maven-plugin

相关文章

网友评论

      本文标题:Json Schema 及 JsonSchema2pojo ma

      本文链接:https://www.haomeiwen.com/subject/yytfphtx.html