我正在尝试在JSON中生成的Java代码中包含Spring Boot注释,如下所示:
@Entity
public class Person {
...
}
和
@Repository
public interface PersonRepository extends CrudRepository<Person, Long>
{
}
我正在使用this tutorial从JSON转换为POJO。我可以添加到我的json文件中以使生成的Java类包含注释@Entity和@Repository?我还没有找到关于如何提供自定义注释的教程或解释。
jsonschema2pojo看起来在生成类时可以使用自定义注释器,但我想知道杰克逊内置的内容是否可以轻松实现自定义注释?
答案 0 :(得分:2)
jsonschema2pojo的customAnnotator允许我为生成的java文件添加自定义注释。令人烦恼的是,您的注释器类必须位于单独的项目中,并且必须包含在插件中。 Here's why。
将依赖项添加到pom.xml
<dependency>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-core</artifactId>
<version>0.4.0</version>
</dependency>
将插件添加到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>
在单独的项目中创建自定义注释器类。
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);
}
}
}
最后,json文件:
{
"title": "Person",
"type": "object",
"properties": {
"entity": true,
"name": {
"type": "string"
}
}
}
最终结果:
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;
...
}
我希望有一种更简单的方法可以做到这一点。
答案 1 :(得分:1)
就我而言,我需要按照https://www.baeldung.com/javax-validations-enums(第5个选项)中的建议添加Output: [{"id":20, "title":"Title 1", "body":" first post ", "showcase_image": 'first_image.jpg' , "user_id":1 }]
所以我调整了覆盖方法
@ValueOfEnum(enumClass = com.mycompany.SampleType)
还要确保ValueOfEnum,并且插件可以使用实际的枚举。在pom.xml中添加插件依赖项
答案 2 :(得分:0)
我正在猜测问题背后的意图:
也许尝试用JPA注释装饰生成的POJO的一种替代方法是使用orm.xml
。这可用于定义实体并与您的ORM集成,以作为注释POJO的替代方法。这对于您自己无法进行更改的现有类或在这种情况下(例如在生成代码的情况下)很有用。
使用您生成的POJO,将orm.xml
添加到您的${project.basedir}/src/main/resources/META-INF
代替添加注释
也许类似
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<entity class="com.test.gen.Person" access="FIELD">
<table name="person"/>
<attributes>
<id name="id">
<generated-value strategy="AUTO"/>
</id>
<basic name="firstname">
<column name="firstname" length="200"/>
</basic>
</attributes>
</entity>
</entity-mappings>