java - Spring Data Rest - 隐藏而不是暴露 ID

标签 java spring-boot jackson entity

我有一个实体类,它有一个映射为@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/

相关文章:

Java变量的作用域和生命周期

java - JAVA中如何从字符串中获取时区

java - 如何确定 2 个圆圈是否重叠

mysql - Spring Boot - Hibernate 未正确保存日期

java - 尝试在 Java 中创建 json 对象但获取嵌套数组?如何以更好的方式做到这一点?

java - Jackson - 无法反序列化带有时区偏移量的日期时间 'unparsed text found at index 23'

java - 将根元素添加到生成的 json 对象中

java - 用于关闭 JDialog 的按钮

java - cvc-complex-type.2.4.c:匹配的通配符是严格的,但是找不到元素 'hdp:configuration'的声明

java - Modelmapper 无法映射整个模型?