Home > Java > javaTutorial > How Can I Generate Java Classes from JSON Data Using the jsonschema2pojo Maven Plugin?

How Can I Generate Java Classes from JSON Data Using the jsonschema2pojo Maven Plugin?

Patricia Arquette
Release: 2024-11-28 21:10:16
Original
450 people have browsed it

How Can I Generate Java Classes from JSON Data Using the jsonschema2pojo Maven Plugin?

Can You Generate Java Classes from JSON?

As a Java developer, you may encounter situations where you need to generate Java source files from JSON data. This can be a valuable technique for creating data transfer objects (DTOs) or POJOs (Plain Old Java Objects) that mirror JSON structures.

Here's how to accomplish this using the jsonschema2pojo Maven plugin:

Maven Plugin Configuration

  1. Add the following plugin configuration to your pom.xml file:
<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>
Copy after login
  • sourceDirectory: Specifies the location of your JSON schema files.
  • targetPackage: Defines the package name for your generated classes.
  • sourceType: Indicates that the source is JSON (remove this line if using JSON schemas).

Example JSON Data

Consider the following JSON data:

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

Generated Java Classes

Upon running the Maven plugin, the following Java classes will be generated:

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"));
    }
}
Copy after login

These generated classes provide easy access to the data in the JSON structure, enabling you to work with the data in a convenient and object-oriented manner.

The above is the detailed content of How Can I Generate Java Classes from JSON Data Using the jsonschema2pojo Maven Plugin?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template