zoukankan      html  css  js  c++  java
  • jsonschema2pojo 使用

    jsonschema2pojo 是一个java的json转bean的工具包.由于json本身就很灵活和普及,使用它来生成java bean实体也比较方便.

    使用方式:

    插件方式
    通过依赖自己实现bean的生成

    1. 插件方式

    前置条件:

    address.schema.json 文件放到 src/main/resources/schema 目录下.

    在maven的pom.xml中增加插件配置:

            <plugins>
                <plugin>
                    <groupId>org.jsonschema2pojo</groupId>
                    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                    <version>1.1.1</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>
    

    增加maven编译target插件,不同jdk版本要修改configuration的版本:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
    

    增加maven依赖:

    commons-lang:用于生成equals, hashCode and toString方法
    jackson-databind: 用于注解解析

            <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>
    

    然后执行 mvn package 命令,就可以获取到 target/java-gen/com/example/types/Address.java 文件.

    1. 嵌入方式

    插件方式使用比较简单,但是需要预先放置需要转换的json文件,实际使用中这种方式不是很灵活.嵌入方式直接可以基于 包来自定义实现json来源和输出方式.可以灵活的支持实际的业务需求.

    引入maven依赖

            <dependency>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-core</artifactId>
                <version>1.1.1</version>
            </dependency>
    

    进行转换定义:

    JCodeModel codeModel = new JCodeModel();
    
    // 获取实际需要转换的json文件
    URL source = Example.class.getResource("/schema/required.json");
    
    GenerationConfig config = new DefaultGenerationConfig() {
    @Override
    public boolean isGenerateBuilders() { // set config option by overriding method
    return true;
    }
    };
    
    SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
    mapper.generate(codeModel, "ClassName", "com.example", source);
    
    codeModel.build(Files.createTempDirectory("required").toFile());
    

    最终生成的文件在 target/classes/com/example/AddressSchema.java
    文中主要基于maven方式,如果使用ant或者没有使用maven可以查阅参考资料,进行实现.

    参考资料

    https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started

    喜欢关注一下,不喜欢点评一下
  • 相关阅读:
    Linux Context , Interrupts 和 Context Switching 说明
    zabbix监控cpu jumps
    国际时区 TimeZone ID列表
    onenote无法更新,提示无法流式传输、无法登陆等问题解答
    Laravel Lumen 数组操作
    ApiDoc 和 Swagger 接口文档
    现代 PHP 新特性系列
    php 流(Stream)
    laravel Lumen邮箱发送配置
    钉钉开发验证登录功能
  • 原文地址:https://www.cnblogs.com/chengmuyu/p/15474164.html
Copyright © 2011-2022 走看看