[英]Spring Boot POST request to endpoint returning Null values
I am sending the request payload from post man.我正在从邮递员发送请求有效负载。 I want some other fields in the entity to store null value until another action is performed (Activate) but for other fields that I send their values , I get a Null response and for other fields that values set returns 0.0
我希望实体中的其他一些字段存储空值,直到执行另一个操作(激活),但对于我发送其值的其他字段,我得到 Null 响应,而对于其他设置值的字段返回 0.0
This is the model这是模型
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 ;
This is the service class containing the business logic for creating investment.这是包含用于创建投资的业务逻辑的服务类。
public Investment CreateInvestment(Investment investment)
{
investment.setAccountNumber(nextAccountNumber());
investment.setStatus(InvestmentStatus.pending);
return investmentRepository.save(investment);
}
This is the controller class code ,这是控制器类代码,
@PostMapping(value="/createInvestment")
public @ResponseBody Investment CreateInvestment (Investment investment)
{
investmentService.CreateInvestment(investment);
return investment;
}
This is the request payload on postman send to the endpoint to create investment account.这是邮递员发送到端点以创建投资帐户的请求负载。
{
"category": "Corporafe",
"principal":65000,
"tenure": "30",
"investmentPackage": "Premium Investment",
"rate": 12,
"whtStatus": "Yes",
"customer" : {"id": 14}
}
This is the response payload gotten after making a post request to the endpoint这是向端点发出发布请求后获得的响应负载
{
"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
}
Updated : this is the repository class更新:这是存储库类
@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);
Update I added @RequestBody to the post request arguments .更新我将 @RequestBody 添加到 post 请求参数。 Now the selected fields are being populated but the Customer details response displays null for all fields .
现在正在填充所选字段,但客户详细信息响应为所有字段显示 null。
{
"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
}
}
好的,我通过将 @RequestBody 添加到控制器 Method 来解决此问题,然后在使用 FetchType Lazy 的投资类中将 CascadeType.ALL 更改为 CascadeType.MERGE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.