Spring Boot POST请求端点返回空值



我正在从post man发送请求有效负载。我希望实体中的其他一些字段存储空值,直到执行另一个操作(激活),但对于我发送其值的其他字段,我得到null响应,对于值集返回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);


我在post请求参数中添加了@RequestBody。现在正在填充所选字段,但是Customer details响应对所有字段都显示为空。

{
"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
}
}

Ok,我通过将@RequestBody添加到控制器方法来修复此问题,然后更改cascadtype。ALL to CascadeType。在使用了FetchType Lazy的投资类中合并。

最新更新