Home > Mobile >  How to send a Request body in postman for Embedded entity class in spring data jpa?
How to send a Request body in postman for Embedded entity class in spring data jpa?

Time:08-01

Can anyone please help me on how to send a nested json requestbody in postman for the below entity class.

ProcessRequestInfo.java

import java.sql.Date;
import java.time.ZonedDateTime;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="PROCESS_REQUEST_INFO")
public class ProcessRequestInfo {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int id;
    private String userName;
    private String contactNumber;
    @Embedded
    private DefectiveComponentInfo defectiveComponentInfo;
     
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getContactNumber() {
        return contactNumber;
    }
    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }
    public DefectiveComponentInfo getDefectiveComponentInfo() {
        return defectiveComponentInfo;
    }
    public void setDefectiveComponentInfo(DefectiveComponentInfo defectiveComponentInfo) {
        this.defectiveComponentInfo = defectiveComponentInfo;
    }
    
    @Override
    public String toString() {
        return "ProcessRequestInfo [id="   id   ", userName="   userName   ", contactNumber="   contactNumber
                  ", defectiveComponentInfo="   defectiveComponentInfo   "]";
    }
    public ProcessRequestInfo(int id, String userName, String contactNumber,
            DefectiveComponentInfo defectiveComponentInfo, ZonedDateTime date) {
        super();
        this.id = id;
        this.userName = userName;
        this.contactNumber = contactNumber;
        this.defectiveComponentInfo = defectiveComponentInfo;
    }
    public ProcessRequestInfo() {
        super();
        // TODO Auto-generated constructor stub
    }
         
     
     
}

DefectiveComponentInfo.java

import javax.persistence.Embeddable;

@Embeddable
public class DefectiveComponentInfo {
    
    private String componentType;
    private String componentName;
    private Long quantity;
    private String description;
    
    public String getComponentType() {
        return componentType;
    }
    public void setComponentType(String componentType) {
        this.componentType = componentType;
    }
    public String getComponentName() {
        return componentName;
    }
    public void setComponentName(String componentName) {
        this.componentName = componentName;
    }
    public Long getQuantity() {
        return quantity;
    }
    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    
    @Override
    public String toString() {
        return "DefectiveComponentDetail [componentType="   componentType   ", componentName="   componentName
                  ", quantity="   quantity   ", description="   description   "]";
    }
    
    public DefectiveComponentInfo(String componentType, String componentName, Long quantity, String description) {
        super();
        this.componentType = componentType;
        this.componentName = componentName;
        this.quantity = quantity;
        this.description = description;
    }
    
    public DefectiveComponentInfo() {
        super();
        // TODO Auto-generated constructor stub
    }
    

}

I have tried few nested json requestbody in postman but the inner class data is getting null. Below is my json request body which I have tried.

{
    "userName" : "sam",
    "contactNumber" : "96014587555",
    "DefectiveComponentInfo": 
    [  {
        "componentType":"Integral",
        "componentName":"Bummper",
        "quantity":5,
        "description": "Repair product"
      }]
  }

Thanks in advance!!!

CodePudding user response:

Try this:

"DefectiveComponentInfo": 
{
    "componentType": "Integral",
    "componentName": "Bummper",
    "quantity": 5,
    "description": "Repair product"
}

Because you're using [] but these indicate a list.

CodePudding user response:

You are almost there. few things to be corrected

{
    "userName" : "sam",
    "contactNumber" : "96014587555",
    "defectiveComponentInfo": {
      "componentType":"Integral",
      "componentName":"Bummper",
      "quantity":5,
      "description": "Repair product"
    }
}

What i have corrected here

  1. As per @Ausgefuchster already mentioned about the incorrect usage of [] array syntax. Removed it.
  2. Your property name is defectiveComponentInfo not capital D.
  • Related