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

3
我是您的助手,翻译如下:

我有一个实体类,它有一个自然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.


如果有帮助的话,请查看以下这些文章。 https://dev59.com/pGAg5IYBdhLWcg3w1uDy https://dev59.com/bGcs5IYBdhLWcg3w433H - surendrapanday
尝试对 @JsonProperty("bin") 进行注释并检查。 - Ashok Kumar N
将属性注释为 @JsonProperty(access = Access.WRITE_ONLY) private String bin; - Yogi
非常晚的评论:我猜测神秘的Id字段源自Persistable接口。 - undefined
4个回答

0
感谢大家,我认为这与Spring或Jackson的某些配置有关,它将自动公开使用@Id映射的字段。 我只能猜测,因为没有时间进行确认。
一些同事建议我定义DTO而不是在类中放置@Jsonxxx注释,称模型表示数据模型并与表相关,而DTO与视图层相关。 所以我这样做了,现在一切都很好。
现在,该模型不再包含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中,并将其返回到端点。然后JSON是正常和良好的。

0
尝试使用@NaturalId进行注释,而不是@Id

不,它不起作用。如果我只使用NaturalID,它会抱怨“没有标识符”。如果我在同一个字段上使用@Id@NaturalID,我仍然会在JSON中得到id键。 - WesternGun
所以您使用代理键并在其上放置json ignore。然后将其用作bin上的@NaturalId?对于此解决方法不太确定。似乎必须有更有效的解决方案。 - x86-64
我没有添加代理ID;我只是尝试在“bin”上添加@Id@NaturalId,但是(1)使用@NaturalID;(2)使用ID和NaturalID,它们都不起作用。 - WesternGun
我建议的是这个:https://gist.github.com/aliahsan07/c6b949d71f775a7538063bcdfe68c96d - x86-64

0
你需要实现获取和设置id字段的功能。

1
当前您的答案写得不够清晰,请[编辑]以添加更多细节帮助其他人了解它如何回答所提出的问题。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

-2

尝试使用@JsonIgnore注释该字段


嗯...我想在JSON中显示bin,而且没有什么可以忽略的,因为id不是bean中的字段。 - WesternGun
由于这是一个Spring Data Rest项目(意味着您的数据实体是API对象响应),您可能需要创建一个过滤器来解析ID字段。 - alltej

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接