我有一个实体类,它有一个映射为@Id
的自然ID字段,并且我没有任何代理ID(仅为表ID发明的字段)字段。而且,在 Jackson 编码的 JSON 中,我看到一个额外的 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。
最佳答案
尝试使用 @NaturalId
而不是 @Id
进行注释。
关于java - Spring Data Rest - 隐藏而不是暴露 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52039545/