我有一个实体类,它有一个映射为@Id的自然ID字段,而我没有任何代理ID(仅用于表ID的虚构字段)字段。而且,在Jackson marshalled中,我看到了一个额外的id。
因此,不是:
{
"bin":"123456", ...
}我明白了:
{
"id":"123456", "bin":"123456", ...
}我不想要,因为它们是重复的信息。我如何防止这种情况发生?
我还没有接触REST/MVC配置适配器;它们用于公开ID类,但我不希望这样。
Bean:
@Entity
@Data
@Table(name="bin_info")
public class BinInfo implements Serializable, Persistable<String> {
@Id
@NotBlank //this is for absent parameter. Not equal to Pattern regex check
@Pattern(regexp = "^\\d{6,8}$") //6-8 digits
@Column(name="bin")
@JsonProperty("bin")
private String bin;
...我有以下依赖关系:
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-undertow')
runtime('com.h2database:h2')
runtime('org.postgresql:postgresql')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.cucumber:cucumber-java:3.0.2')
testCompile('io.cucumber:cucumber-junit:3.0.2')
testCompile('io.cucumber:cucumber-spring:3.0.2')
}Spring Boot 2.0.3。
发布于 2018-08-29 04:36:26
感谢所有人,我认为这与Spring或Jackson的一些配置有关,这些配置将自动公开用@Id映射的字段。我只能猜测,因为没有时间进行确认。
一些同事建议我定义一个数据对象,而不是将@Jsonxxx注释放在类中,他们说模型代表数据模型,与表相关,而数据对象与视图层相关。所以我这么做了,现在一切都好了。
现在模型中没有了id字段和@JsonProperty/@JsonIgnore
@Entity
@Data
@Table(name="bin_info")
public class BinInfo implements Serializable, Persistable<String> {
@Id
@NaturalId
@NotBlank //this is for absent parameter. Not equal to Pattern regex check
@Pattern(regexp = "^\\d{6,8}$") //6-8 digits
@Column(name="bin")
//@JsonProperty("bin")
private String bin;
...DTO完全没有@Id
@Data
public class BinInfoDTO {
@JsonProperty("bin")
private String bin;
@JsonProperty("json_full")
private String json_full;
...在检索实体时,使用映射方法将DTO中需要的所有值设置为DTO,并将其返回给端点。那么JSON就是正常的和正常的。
发布于 2018-08-27 20:45:53
尝试使用@NaturalId而不是@Id进行注释。
发布于 2018-08-27 21:02:20
尝试使用@JsonIgnore注释该字段
https://stackoverflow.com/questions/52039545
复制相似问题