3

I have en entity class which has an natural ID field mapped as @Id and I don't have any surrogate ID(invented field only for table ID) field. And, in the Jackson marshalled JSON I see an extra id exposed.

So instead of:

{
    "bin":"123456", ...
}

I see:

{
    "id":"123456", "bin":"123456", ...
}

which I don't want because they are repeated information. How can I prevent this?

I haven't touched REST/MVC configuration adapter; they are for exposing the ID classes, but I don't want that.

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;

    ...

I am with these dependencies:

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.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • Check these post if it helps you. https://stackoverflow.com/questions/23101260/ignore-fields-from-java-object-dynamically-while-sending-as-json-from-spring-mvc https://stackoverflow.com/questions/12505141/only-using-jsonignore-during-serialization-but-not-deserialization – surendrapanday Aug 27 '18 at 13:09
  • try to comment @JsonProperty("bin") and check – Ashok Kumar N Aug 27 '18 at 13:12
  • Annotate property as @JsonProperty(access = Access.WRITE_ONLY) private String bin; – Yogi Aug 27 '18 at 13:24

4 Answers4

0

Try annotating with @NaturalId instead of @Id.

Alien
  • 15,141
  • 6
  • 37
  • 57
x86-64
  • 365
  • 1
  • 5
  • 16
  • No, it does not work. If I only uses `NaturalID`, it complains about "No identifier". If I use `@Id` and `@NaturalID` on the same field, I still get `id` key in the JSON. – WesternGun Aug 27 '18 at 13:43
  • So you use a surrogate key and place json ignore on it. And then use this as `@NaturalId` on bin? Not too sure about this workaround. Seems like there must be a efficient solution – x86-64 Aug 27 '18 at 14:09
  • I didn't add a surrogate ID; I just tried to add `@Id` and `@NaturalId` on `bin`, but (1)with @NaturalID; (2) with ID and NaturalID, they neither work. – WesternGun Aug 27 '18 at 14:28
  • What I was suggesting was this: https://gist.github.com/aliahsan07/c6b949d71f775a7538063bcdfe68c96d – x86-64 Aug 28 '18 at 06:00
0

Thanks all, I think it has to do with some configuration of Spring or Jackson that will automatically expose the field mapped with @Id. I can just guess because no time for a confirmation.

And some colleague suggests me to define a DTO instead of putting the @Jsonxxx annotations in the class, saying the model represents data model and are related to the table, while DTO is related with view layer. So I did it and now all is fine.

Now the model is free of id field and @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;

    ...

And the DTO is totally without @Id:

@Data
public class BinInfoDTO {
    @JsonProperty("bin")
    private String bin;

    @JsonProperty("json_full")
    private String json_full;

    ...

When I retrieve an entity, with a mapping method I set all values I need in a DTO to the DTO and return it to the endpoint. Then the JSON is normal and fine.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

You have to implement the get and set from id field.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 11 '22 at 17:31
-2

Try annotating that field with @JsonIgnore

alltej
  • 6,787
  • 10
  • 46
  • 87
  • Hm... I want to show `bin` in the json... and there is nothing to ignore because `id` is not a field in the bean. – WesternGun Aug 27 '18 at 13:28
  • Since this is a Spring Data Rest project (meaning your data entities are your api object responses), you might have to create a filter to parse the ID fields. – alltej Aug 28 '18 at 13:47