【发布时间】:2021-12-05 21:16:40
【问题描述】:
由于我是 springboot 和 mongodb 的新手,所以我使用了 https://start.spring.io/ 并生成了一个具有以下设置的演示项目。
然后创建如下模型:
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不一样?如何获取与JsonProperty 相同的数据库集合属性名称?
【问题讨论】:
标签: mongodb spring-boot jackson lombok