java - Spring boot @RequestBody 在保存到数据库之前添加属性
问题描述
我正在使用springboot
和mongodb
。我有一个Customer
模型如下:
package com.example.customerapi.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 org.springframework.data.mongodb.core.mapping.Field;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@Configuration
@Document(collection = "customer")
@JsonInclude(Include.NON_NULL)
public class Customer implements Serializable {
private static final long serialVersionUID = 6748432793461621268L;
@JsonProperty("customer_id")
private String customerId;
@Field("type")
private String type;
@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;
}
在这里,type
不是 的一部分RequestBody
。请在下面找到CustomerController
。
package com.example.customerapi.resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.example.customerapi.repository.CustomerRepository;
import com.example.model.Customer;
import com.example.customerapi.dto.CustomerResponse;
@RestController
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@PostMapping(value="/addCustomer", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<CustomerResponse> saveCustomer(@RequestBody Customer customer) {
try {
customer.setType("test_type"); //this is not working
return new ResponseEntity<CustomerResponse>(new CustomerResponse(customerRepository.save(customer)), HttpStatus.CREATED);
}catch(Exception ex) {
return new ResponseEntity<CustomerResponse>(new CustomerResponse("Error saving customer"), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
type
但在将客户保存到数据库之前,我想设置Customer
一个示例字符串。
如何设置不属于的对象属性RequestBody
?
解决方案
在上面使用@transient
@Transient
private String type;
该字段将被忽略
推荐阅读
- python - 如何通过 python sdk 下载传递内容(视频)?
- javascript - 在上传到谷歌云存储之前用锐利调整图像大小
- c# - 无法通过 pl/sql 在 Oracle 中的请求得到答案
- openstreetmap - 使用 QGIS 或 Maperitive 在 Openstreet Map 上生成 XYZ Tiles 获取坏图像
- azure - Azure B2C 自定义策略 - 有没有办法在 ClaimsProviderSelection 编排步骤中显示声明
- java - Apache Geode JSON PDX 问题
- c++ - 在 C++ 中生成具有相等概率的随机数
- angular - 是否有用于检测未使用的类属性的 eslint 规则?
- sql - 如何在 azure devops 发布管道中包含更新查询
- javascript - 如何测试中间件中返回的请求值