How can I generate Java classes from a JSON schema in a Maven project?
Nov 20, 2024 pm 01:33 PMGenerating 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:
- Use JSON Schema: For a more robust solution, consider using JSON Schema, which provides a formal definition of the JSON's structure.
-
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>
Copy after loginSet 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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

Node.js 20: Key Performance Boosts and New Features
