JsonSchemaPojo

JAVA POJO Class Generate with jsonSchemaUrl Annotation.

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.github.whsoul
ArtifactId

ArtifactId

json-schema-pojo
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

JsonSchemaPojo
JAVA POJO Class Generate with jsonSchemaUrl Annotation.
Project URL

Project URL

https://github.com/whsoul/json-schema-pojo
Source Code Management

Source Code Management

https://github.com/whsoul/json-schema-pojo

Download json-schema-pojo

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.whsoul/json-schema-pojo/ -->
<dependency>
    <groupId>com.github.whsoul</groupId>
    <artifactId>json-schema-pojo</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.whsoul/json-schema-pojo/
implementation 'com.github.whsoul:json-schema-pojo:0.1.0'
// https://jarcasting.com/artifacts/com.github.whsoul/json-schema-pojo/
implementation ("com.github.whsoul:json-schema-pojo:0.1.0")
'com.github.whsoul:json-schema-pojo:jar:0.1.0'
<dependency org="com.github.whsoul" name="json-schema-pojo" rev="0.1.0">
  <artifact name="json-schema-pojo" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.whsoul', module='json-schema-pojo', version='0.1.0')
)
libraryDependencies += "com.github.whsoul" % "json-schema-pojo" % "0.1.0"
[com.github.whsoul/json-schema-pojo "0.1.0"]

Dependencies

compile (6)

Group / Artifact Type Version
com.squareup : javapoet jar 1.9.0
com.google.auto.service : auto-service jar 1.0-rc1
com.fasterxml.jackson.core : jackson-core jar 2.9.5
com.fasterxml.jackson.core : jackson-databind jar 2.9.5
javax.validation : validation-api jar 2.0.0.Final
org.slf4j : slf4j-api jar 1.7.25

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.20

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
org.hamcrest : hamcrest-all jar 1.3
junit : junit jar RELEASE

Project Modules

There are no modules declared in this project.

Englsh Korean

JAVA POJO Class Generate with jsonSchemaUrl Annotation

json-schema-pojo is a library that automatically generate JAVA POJO class file on compile time. (like lombok library)

Just add maven repository dependency,

    <dependency>
        <groupId>com.github.whsoul</groupId>
        <artifactId>json-schema-pojo</artifactId>
        <version>X.Y.Z</version>
    </dependency>

and make config class With Annotations,

then Java compiler make POJOs along annotation configs.

Base Use

public class ConfigClass {

    @JsonSchemaPojo(packageName = "com.test.pojo"
                    , className = "Datas"
                    , schemaUrl = "https://whsoul.github.io/json-schema-pojo/test-schema01.json")
    public String fromExternalResource;

    @JsonSchemaPojo(packageName = "com.test.pojo2"
                    , className = "Sample"
                    , schemaUrl = "sample_schema_01.json")
    public String fromOwnResource;

there are 2 kind of options with target schema

  1. internet resource reference ( offline mode, not available )
  1. my own(or downloaded) schema ( under src/main/resources )
  • sample_schema_01.json

And also you can inject custom configurations, such as below

Base Config Enable/Disable

    @JsonSchemaPojo(packageName = "com.test.sample1", className = "Student"
                    , schemaUrl = "sample_schema_01.json"
                    , baseConfig = {
                        @JsonSchemaPojo.BaseConfig(
                                feature = JSONSCHEMA_POJO_FEATURE.STRING_ENUM_AS_ENUM
                                ,enable = true
                        )
                    })
    public String enableDisableBaseConfig;

Control library generate default actions with config enable/disable

Customize Type Mapping

    @JsonSchemaPojo(packageName = "com.test.sample1", className = "Student"
                    , schemaUrl = "sample_schema_01.json"
                    , typeMappingConfig = {
                        @JsonSchemaPojo.TypeMappingConfig(
                                typeName = "string"
                                , typeJavaClass = MyString.class
                        ),
                        @JsonSchemaPojo.TypeMappingConfig(
                                typeName = "integer"
                                , typeJavaClass = int.class
                        ),
                        @JsonSchemaPojo.TypeMappingConfig(
                                typeName = "customFloat"
                                , typeJavaClass = float.class
                        )
                    }
    )
    public String sampleClass1;

Standard json schema specification support only simpleTypes below

type default mapping
string String.class
boolean boolean.class
long long.class
integer Integer.class
number Double.class
array List.class
object Map.class
null Void.class

but customType supported. override default mapping also

Customize Validation Annotation Config

    @JsonSchemaPojo(packageName = "com.test.sample1", className = "Student"
                    , schemaUrl = "sample_schema_01.json"
                    , annotationConfig = {
                        @JsonSchemaPojo.AnnotationConfig(
                                type = ANNOTATION_TYPE.FIELD_REQUIRED
                                , annotationClass = MyNotNull.class
                        )
                    }
    )
    public String sampleClass1;

Add Interface, Superclass with

    @JsonSchemaPojo(packageName = "com.test.sample1", className = "Student"
                    , schemaUrl = "sample_schema_01.json"
                    ,superclassConfig = {
                        @JsonSchemaPojo.SuperConfig(
                                superclass = A.class
                                , targetClassNames = "com.test.sample1.Student" +
                                                     ",com.test.sample1.definitions.University" +
                                                     ",com.test.sample1.definitions.University.Location"
                        )
                    }
                    ,superinterfaceConfig = {
                        @JsonSchemaPojo.SuperConfig(
                                superclass = BInterface.class
                                , targetClassNames = "com.test.sample1.Student" +
                                ",com.test.sample1.definitions.University" +
                                ", com.test.sample1.definitions.University.Location "
                        )
                    }
    )
    public String sampleClass1;

if you want to add superclass or interface to generated classes, you can use superclassConfig, superinterfaceConfig

Usage Example

API Specifications exchange for caller(client with java)

Java base (Client - Server), (Server - Server) easily exchanges Request, Response data format.

API Server Application

  1. Develop Java API Application with json Request, Response
  2. Generate jsonSchema file (Request Model, Response Model)
  3. Create automatically generate with 3rd party POJO to Json schema library below Reference(#1) ( or Create jsonSchema Structure by your self)
  4. Deploy the static files with your API Server application, or other static CDN (example] http://your.application.domain/my-api001-REQUEST-schema.json, ... my-api001-RESPONSE-schema.json )
  5. Let client know your Server API url with and the schema file download URL

Java Client

  1. Add this library at pom or gradle

  2. Add @JsonSchemaPojo in your code, and schema download url

    @JsonSchemaPojo(packageName = "com.test.pojo"
                    , className = "Api001RequestPojo"
                    , schemaUrl = "http://your.application.domain/my-api001-REQUEST-schema.json")
    public String yourApi001Request;

References

Jackson Json Schema Module (#1)

JSON Schema Draft v3

https://github.com/FasterXML/jackson-module-jsonSchema

JSON Schmea Draft v4

https://github.com/mbknor/mbknor-jackson-jsonSchema

Versions

Version
0.1.0