带有自定义注释的 JSON 到 POJO

Gaʀ*_*ʀʀʏ 7 java json spring-boot jsonschema2pojo

我试图在我从 JSON 生成的 Java 代码中包含 Spring Boot 注释,如下所示:

@Entity
public class Person {
...
}
Run Code Online (Sandbox Code Playgroud)

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> 
{
}
Run Code Online (Sandbox Code Playgroud)

我正在使用本教程从 JSON 转换为 POJO 。我可以在我的 json 文件中添加什么来使生成的 Java 类包含注释 @Entity 和 @Repository?我还没有找到关于如何提供自定义注释的教程或解释。

jsonschema2pojo看起来在生成类时可以使用自定义注释器工作,但我想知道 Jackson 是否有任何内置的东西可以轻松允许自定义注释?

Gaʀ*_*ʀʀʏ 9

jsonschema2pojo 的 customAnnotator 允许我向生成的 java 文件添加自定义注释。它的烦恼在于您的注释器类必须在一个单独的项目中并且必须包含在插件中这就是为什么

将依赖项添加到您的 pom.xml

<dependency>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-core</artifactId>
    <version>0.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

将插件添加到 pom.xml 插件

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>0.5.1</version>
    <dependencies>
        <!-- NOTE: Your annotator MUST come from a dependency -->
        <dependency>
            <groupId>ANNOTATOR_GROUP_ID</groupId>
            <artifactId>ANNOTATOR_ARTIFACT</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>compile</scope>
            <version>1.5.2.RELEASE</version>
        </dependency>
       <!-- NOTE: Any annotation used must have its dependency here!!! -->
    </dependencies>
    <configuration>
        <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
        <targetPackage>com.test.gen</targetPackage>
        <useCommonsLang3>true</useCommonsLang3>
        <customAnnotator>com.fully.qualified.path.YourAnnotator</customAnnotator>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

单独的项目中创建您的自定义注释器类。

package com.deere.gtin_k.pdeaas.work_manager.application;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import org.jsonschema2pojo.AbstractAnnotator;

import javax.persistence.Entity;

public class HibernateAnnotator extends AbstractAnnotator {

    @Override
    public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
        super.propertyField(field, clazz, propertyName, propertyNode);

        // Note: does not have to be the propertyName, could be the field or propertyNode that is verified.
        if (propertyName.equals("entity")) {
            clazz.annotate(Entity.class);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,json文件:

{
  "title": "Person",
  "type": "object",
  "properties": {
    "entity": true,
    "name": {
      "type": "string"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

最后的结果:

package com.test.gen;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.Entity;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;


/**
 * Person
 * <p>
 * 
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Entity
@JsonPropertyOrder({
    "entity",
    "name"
})
public class Person {

    @JsonProperty("entity")
    private Object entity;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我希望有一种更简单的方法来做到这一点。