Home >Java >javaTutorial >How to Generate Java Classes from JSON Using the jsonschema2pojo Maven Plugin?

How to Generate Java Classes from JSON Using the jsonschema2pojo Maven Plugin?

Barbara Streisand
Barbara StreisandOriginal
2024-12-09 04:45:12703browse

How to Generate Java Classes from JSON Using the jsonschema2pojo Maven Plugin?

How to Generate Java Classes from JSON in Maven

Generating Java source files from JSON is a valuable technique for object-to-JSON mapping and data serialization. In this scenario, we seek to create Java classes resembling the following:

class Address {
  private JSONObject mInternalJSONObject;

  Address(JSONObject json) {
    mInternalJSONObject = json;
  }

  String getStreetAddress() {
    return mInternalJSONObject.getString("streetAddress");
  }

  String getCity() {
    return mInternalJSONObject.getString("city");
  }
}

class Person {
  private JSONObject mInternalJSONObject;

  Person(JSONObject json) {
    mInternalJSONObject = json;
  }

  String getFirstName() {
    return mInternalJSONObject.getString("firstName");
  }

  String getLastName() {
    return mInternalJSONObject.getString("lastName");
  }

  Address getAddress() {
    return new Address(mInternalJSONObject.getJSONObject("address"));
  }
}

To achieve this generation in a Maven project, you can leverage a comprehensive tool such as http://www.jsonschema2pojo.org. Alternatively, you can utilize the jsonschema2pojo plug-in for Maven:

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

For JSON sources, you can specify json. However, if you possess actual JSON schemas, this line is unnecessary.

In recent years, the JSON Schema specification has advanced significantly, offering a robust mechanism for defining structural rules. Furthermore, the jsonschema2pojo project provides a purpose-built tool that converts JSON schema documents into Java DTO classes. While still under development, it covers essential portions of the JSON schema. User feedback is crucial for its ongoing evolution, which you can provide through the command line or Maven plugin.

The above is the detailed content of How to Generate Java Classes from JSON Using the jsonschema2pojo Maven Plugin?. 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