Steps to Generate POJOs
Add Dependency Add the
jsonschema2pojo
plugin to yourpom.xml
:
<plugin><groupId>org.jsonschema2pojo</groupId><artifactId>jsonschema2pojo-maven-plugin</artifactId><version>1.1.2</version><executions><execution><goals><goal>generate</goal></goals></execution></executions><configuration><sourceDirectory>${basedir}/src/main/resources/schemas</sourceDirectory><targetPackage>com.example.pojo</targetPackage></configuration></plugin>
Prepare JSON Schemas
Save your schemas in src/main/resources/schemas
. For example:
user.json
references: {
"type": "object","properties": {"id": { "type": "integer" },"name": { "type": "string" },"address": { "$ref": "address.json" }}}
address.json
:
{
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
}
}
Run the Plugin
Execute the Maven compile
phase to generate the POJOs:
mvn compile -- Output
public class User {
private int id;
private String name;
private Address address;
// Getters and setters...
}
Comments
Post a Comment