首页 > 解决方案 > Spring boot @RequestBody 在保存到数据库之前添加属性

问题描述

我正在使用springbootmongodb。我有一个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

标签: javamongodbspring-boot

解决方案


在上面使用@transient

@Transient
private String type;

该字段将被忽略


推荐阅读