Spring Boot POST 请求返回 Null 值的端点

Posted

技术标签:

【中文标题】Spring Boot POST 请求返回 Null 值的端点【英文标题】:Spring Boot POST request to endpoint returning Null values 【发布时间】:2021-12-04 12:04:00 【问题描述】:

我正在从邮递员发送请求有效负载。我希望实体中的其他一些字段存储空值,直到执行另一个操作(激活),但对于我发送其值的其他字段,我得到空响应,对于值集返回 0.0 的其他字段

这是模型

import java.math.BigDecimal;
import java.time.LocalDate;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Investment 

   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "AccountNumber", updatable = false, nullable = false)
    private int accountNumber;
    private String category;
    private BigDecimal principal;
    //private BigDecimal netInvestmentAmount;
    private BigDecimal currentInterest;
    private BigDecimal maturityInterest;
    private String tenure;
    private String marketer;
    private String investmentPackage;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate startDate;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate maturityDate;
    private int rate;
    private final float wht = 10/100;
    private String whtStatus;
    private final float  preLiquidationCharges = 25/100;
    @Enumerated(EnumType .STRING)
    private InvestmentStatus status;
    
    @JsonIgnoreProperties("hibernateLazyInitializer", "handler")
    @ManyToOne(targetEntity = Customer.class, 
    cascade = CascadeType.ALL, fetch = FetchType.LAZY )
    @JoinColumn(name="customer_fk_id") 
    private Customer customer_id ;

这是包含创建投资的业务逻辑的服务类。

public Investment CreateInvestment(Investment investment)
       
        investment.setAccountNumber(nextAccountNumber());
        investment.setStatus(InvestmentStatus.pending);
         return investmentRepository.save(investment);
      

这是控制器类代码,

 @PostMapping(value="/createInvestment")
    public @ResponseBody Investment CreateInvestment (Investment investment)  
    
     investmentService.CreateInvestment(investment);
     return investment;
    

这是邮递员发送到端点以创建投资账户的请求负载。

    
        "category": "Corporafe",
        "principal":65000,
        "tenure": "30",
        "investmentPackage": "Premium Investment",
        "rate": 12,
        "whtStatus": "Yes",
         "customer" : "id": 14
       

这是向端点发出 post 请求后得到的响应负载


    "id": 7,
    "accountNumber": 2021070417,
    "category": null,
    "principal": null,
    "currentInterest": null,
    "maturityInterest": null,
    "tenure": null,
    "marketer": null,
    "investmentPackage": null,
    "startDate": null,
    "maturityDate": null,
    "rate": 0,
    "wht": 0.0,
    "whtStatus": null,
    "preLiquidationCharges": 0.0,
    "status": "pending",
    "customer": null


更新:这是存储库类

@Repository
public interface InvestmentRepository extends JpaRepository <Investment, Long>

 
    
    Optional<Investment> findById(Long id);
    
    List<Investment> findByStartDateBetween(LocalDate startDate, LocalDate endDate);
    
    
    @Query("SELECT new com.bethsaida.org.models.InvestmentDTO (c.firstName, c.lastName, c.Address, c.category, c.marketer, "
            + "i.accountNumber, i.principal, i.maturityInterest, i.startDate, i.maturityDate, i.status, i.tenure)"
            + " FROM Investment i JOIN i.customer_id c")
    List<InvestmentDTO> getJoinInformation(Long id);
     
    

更新我将@RequestBody 添加到发布请求参数中。现在正在填充所选字段,但客户详细信息响应显示所有字段为空。


    "id": 17,
    "accountNumber": 2021070417,
    "category": "Individual",
    "principal": 50000,
    "currentInterest": null,
    "maturityInterest": null,
    "tenure": "30",
    "marketer": null,
    "investmentPackage": "Classic Investment",
    "startDate": null,
    "maturityDate": null,
    "rate": 12,
    "wht": 0.1,
    "whtStatus": "Yes",
    "preLiquidationCharges": 0.25,
    "status": "pending",
    "customer": 
        "id": 13,
        "firstName": null,
        "lastName": null,
        "gender": null,
        "maritalStatus": null,
        "category": null,
        "motherMaidenName": null,
        "idType": null,
        "idNumber": null,
        "marketer": null,
        "phoneNumber": null,
        "email": null,
        "dateOfBirth": null,
        "registrationDate": null,
        "address": null
    


【问题讨论】:

控制器方法应该是return investmentService.CreateInvestment(investment). @AndrewS 我更改了控制器返回方法,但它仍然返回空值。 看起来accountNumberstatus 正在设置中。 10/100 和 25/100 也是整数除法,结果为 0。'investmentRepository' 有什么作用? @AndrewS 是的 accountNumber 和 status 正在设置中。感谢整数除法通知。我已经更新了帖子以包含 Repository 类。 【参考方案1】:

好的,我通过将 @RequestBody 添加到控制器 Method 来解决此问题,然后在使用 FetchType Lazy 的投资类中将 CascadeType.ALL 更改为 CascadeType.MERGE。

【讨论】:

以上是关于Spring Boot POST 请求返回 Null 值的端点的主要内容,如果未能解决你的问题,请参考以下文章

spring boot应用集成cas单点登录,post请求拿不到参数

执行 POST 请求时的 Spring Boot 端点 403 OPTIONS

Spring boot - 一些 POST 请求严重延迟

缺少请求正文的 Spring Boot POST 请求?

无法将http post请求发送到spring boot

Spring Boot - 模拟对外部 API 的 POST REST 请求