Napstablook
Napstablook

Reputation: 614

Default behaviour of ObjectMapper

I have a class object which I am converting to json using jackson ObjectMapper. After converting it makes double entries for each variable. Is this behaviour normal and if it is then can somebody explain that to me a bit?

My current understanding is that object mapper takes @JsonProperty annotation to create fieldNames

@Data
@Entity
@Table(name = "oracle_blob")
public class OracleBlob {

    @Id
    @GenericGenerator(name = "native", strategy = "native")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @Column(name ="id")
    @JsonIgnore
    private Long id;

    @Column(name ="source_entity")
    @JsonProperty("Source_Entity")
    private String Source_Entity;

    @Column(name ="interface_name")
    @JsonProperty("Interface_Name")
    private String Interface_Name;

    @Column(name ="batch_id")
    @JsonProperty("Batch_Id")
    private String Batch_Id;

    @Column(name ="message")
    @JsonProperty("Message_Content")
    private String Message_Content;

}

Output

{
   "source_Entity":"test source2",
   "interface_Name":"test Interface Name2",
   "batch_Id":"testbatchId2",
   "message_Content":"test message2",
   "Source_Entity":"test source2",
   "Interface_Name":"test Interface Name2",
   "Batch_Id":"testbatchId2",
   "Message_Content":"test message2"
}

Upvotes: 2

Views: 774

Answers (1)

nmyk
nmyk

Reputation: 1622

Do not use Capital letters for field names, because Jackson will parse getters (generated by lombok in your example) and search for interface_name based on getInterface_Name() + one field for @JsonProperty("Interface_Name"), etc.

Jackson 2.5 did add MapperFeature.USE_STD_BEAN_NAMING, enabling of which gets handling you want. It is disabled by default for backwards-compatibility reasons. Another alternative (and only choice if you are using an earlier version) would be to annotate getter with @JsonProperty as well; if so, field and getter would be properly coupled.

See full answer here : https://github.com/FasterXML/jackson-databind/issues/729#issuecomment-84761480

Upvotes: 3

Related Questions