Home > database >  @JsonProperty annotation is getting ignored in mongodb collection
@JsonProperty annotation is getting ignored in mongodb collection

Time:10-19

As I'm new to springboot and mongodb I've used enter image description here

Then created a model as below:

package com.example.model;

import java.io.Serializable;
import java.time.LocalDate;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.mongodb.core.mapping.Document;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Configuration

@Document(collection = "customer")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer implements Serializable {
    
    private static final long serialVersionUID = 6748432793461621268L;

    @JsonProperty("customer_id")
    private String customerId;
    
    @JsonProperty(value= "external_customer_reference_id")
    private String externalCustomerReferenceId;
    
    
    @JsonProperty("title")
    private String title;

    @JsonProperty("first_name")
    private String firstName;
    
    @JsonProperty("middle_name")
    private String middleName;

    @JsonProperty("last_name")
    private String lastName;
    
    @JsonProperty("email")
    private String email;

    @JsonProperty("phone")
    private String phone;

    @JsonProperty("note")
    private String note;
    
    @JsonProperty("date_of_birth")
    private String dateOfBirth;

    @JsonProperty("sex")
    private String sex;
    
    @JsonProperty("contact_address")
    private Address address;
    
    @CreatedDate
    @JsonProperty("create_timestamp")
    private LocalDate createdDate;
    
    @LastModifiedDate
    @JsonProperty("modified_timestamp")
    private LocalDate modifiedDate;
    
    
}

I'm able to save customer in mongodb collection customer. But the collection attribute names are not as same as @JsonProperty("modified_timestamp").

enter image description here

Why db collection attribute names are not as same as JsonProperty? How do I get db collection attribute names as same as JsonProperty?

CodePudding user response:

In MongoDB you're saving an object with his properties names. JsonProperty annotation mapping the deserialization and the serialization with given object.

Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property. Default value ("") indicates that the field name is used as the property name without any modifications, but it can be specified to non-empty value to specify different name. Property name refers to name used externally, as the field name in JSON objects.

https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonProperty.html

By using @Field property annotation you can save property in different name as in object.

  • Related