因為我是新手springboot并且mongodb我已經使用了
然后創建一個模型如下:
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;
}
我能夠將客戶保存在mongodb集合中customer。但是集合屬性名稱與@JsonProperty("modified_timestamp").

為什么 db 集合屬性名稱與JsonProperty? 如何獲得與 db 集合屬性名稱相同的名稱JsonProperty?
uj5u.com熱心網友回復:
在 MongoDB 中,您使用他的屬性名稱保存一個物件。JsonProperty 注釋映射反序列化和給定物件的序列化。
標記注釋可用于將非靜態方法定義為邏輯屬性的“setter”或“getter”(取決于其簽名),或將用作(序列化、反序列化)的非靜態物件欄位作為邏輯屬性財產。默認值("")表示欄位名作為屬性名不做任何修改,但可以指定為非空值來指定不同的名稱。屬性名稱是指外部使用的名稱,作為 JSON 物件中的欄位名稱。
https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonProperty.html
通過使用@Field屬性注釋,您可以以與物件不同的名稱保存屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326160.html
