Home  >  Article  >  Java  >  How can I generate Java classes from a JSON schema in a Maven project?

How can I generate Java classes from a JSON schema in a Maven project?

Linda Hamilton
Linda HamiltonOriginal
2024-11-20 13:33:16801browse

How can I generate Java classes from a JSON schema in a Maven project?

Generating Java Classes from JSON using Maven

Problem:

In a Java Maven project, how can you generate Java source files from a JSON schema? For instance, given the following JSON:

{
  "firstName": "John",  
  "lastName": "Smith",  
  "address": {  
    "streetAddress": "21 2nd Street",  
     "city": "New York"
  }
}

You want to generate Java classes like:

class Address  {
    JSONObject mInternalJSONObject;
     
    Address (JSONObject json){
        mInternalJSONObject = json;
    }
     
    String  getStreetAddress () {
        return mInternalJSONObject.getString("streetAddress");
    }
    
    String  getCity (){
        return mInternalJSONObject.getString("city");
    }
}

class Person {        
    JSONObject mInternalJSONObject;
    
    Person (JSONObject json){
        mInternalJSONObject = json;
    }
    
    String  getFirstName () {
        return mInternalJSONObject.getString("firstName");
    }
    
    String  getLastName (){
        return mInternalJSONObject.getString("lastName");
    }
    
    Address getAddress (){
        return Address(mInternalJSONObject.getString("address"));
    }
}

Solution:

  1. Use JSON Schema: For a more robust solution, consider using JSON Schema, which provides a formal definition of the JSON's structure.
  2. jsonschema2pojo Maven Plugin:

    <plugin>
        <groupId>org.jsonschema2pojo</groupId>
        <artifactId>jsonschema2pojo-maven-plugin</artifactId>
        <version>1.0.2</version>
        <configuration>
            <sourceDirectory>${basedir}/src/main/resources/schemas</sourceDirectory>
            <targetPackage>com.myproject.jsonschemas</targetPackage>
            <sourceType>json</sourceType>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    Set sourceType to json if your source is JSON directly. For JSON schemas, remove this line.

Additional Tips:

  • JSON Schema: If you already have JSON schemas, you can generate Java classes by converting them to org.jsonschema2pojo.model.Schema objects using the com.sun.codemodel.jsonschema.JsonUnboxer class.
  • Custom Code Generation: Use the jsonschema2pojo tool directly to generate Java code, giving you more flexibility but requiring manual changes if the JSON schema changes.

The above is the detailed content of How can I generate Java classes from a JSON schema in a Maven project?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn