• 使用jsonschema2pojo-maven-plugin 插件根据json文件生成代码


    jsonschema2pojo 是一个不错的工具,可以帮助我们快速的根据json 文件生成pojo代码,提高开发效率,以下为简单的
    使用maven 插件进行代码生成

    使用maven 插件配置

    <?xml version="1.0" encoding="UTF-8"?>
    <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.dalong.demo</groupId>
        <artifactId>jsonschemagenerate</artifactId>
        <version>1.0-SNAPSHOT</version>
        <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.5.4</version>
            </dependency>
        </dependencies>
    
       <build>
           <plugins>
               <plugin>
                   <groupId>org.jsonschema2pojo</groupId>
                   <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                   <version>0.5.1</version>
                   <configuration>
                       <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
                       <targetPackage>com.dalong.app</targetPackage>
                       <sourceType>json</sourceType>
                       <generateBuilders>false</generateBuilders>
                   </configuration>
                   <executions>
                       <execution>
                           <goals>
                               <goal>generate</goal>
                           </goals>
                       </execution>
                   </executions>
               </plugin>
           </plugins>
       </build>
    </project>
    

    json 文件

    src/main/resources/schema 目录

    user.json
    {
      "firstName": "John",
      "lastName" : "doe",
      "age"      : 26,
      "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
      },
      "phoneNumbers": [
        {
          "type"  : "iPhone",
          "number": "0123-4567-8888"
        },
        {
          "type"  : "home",
          "number": "0123-4567-8910"
        }
      ]
    }
    app.json
    {
      "appname": "John",
      "apptag" : "doe",
      "appdescribe"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
      },
      "applist": [
        {
          "type"  : "iPhone",
          "number": "0123-4567-8888"
        },
        {
          "type"  : "home",
          "number": "0123-4567-8910"
        }
      ]
    }
    

    生成代码

    mvn  package

    说明

    jsonschema2pojo 功能比较强大,可以根据yaml xml 等文件格式生成pojo,同时可以方便的为我们生成代码,同时也比较灵活
    在api 盛行的时代还是比较好的,同时swagger generator 也是一个不错的选择

    生成结果

    参考资料

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

  • 相关阅读:
    邪恶改装2:用单片机实现一次简单的wifi密码欺骗
    TPYBoard自制微信远程智能温湿度计
    什么是私有网络
    DCHP是什么意思
    ipconfig 命令有什么作用
    什么是网关及网关作用
    什么叫路由
    kvm libvirt 虚拟机管理
    通过python-libvirt管理KVM虚拟机 源码
    通过python-libvirt管理KVM虚拟机 代码实现
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9322345.html
Copyright © 2020-2023  润新知