Saimuga
Saimuga

Reputation: 435

Spring Data Rest API- Entity Object coming as null in Post Request

I have created a spring-data-rest controller (Post Method) which accepts a custom request object named IssueRequiredDetails. This object has another object named IssueDocument as an attribute. The thing is this IssueDocument object is a database entity so it has all the DB Annotations in the class (@Entity, @Table, @Column etc...). When I call this endpoint through postman, all the other fields are properly getting assigned values but the IssueDocument is coming as null. The strange thing is when I remove all the database annotations from the IssueDocument class and hit the same endpoint, then the IssueDocument is coming in properly as part of the postman call. Not sure why its happening and what I need to resolve the issue. I have provided the class and postman request below. Please throw some light on it.

Controller Class

@PostMapping(path="issue-doc-controller")
public ResponseEntity saveDoc(@RequestBody IssueRequiredDetails issueRequiredDetails){

    return ResponseEntity.ok().build();
}

IssueRequiredDetail Class

public class IssueRequiredDetails implements Serializable {


    public IssueRequiredDetails(){

    }

    private String contentType;
    private String filename;
    private byte[] content;
    private Long key;
    private String reader;
    private IssueDocument issueDocument;


    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }

    public String getReader() {
        return reader;
    }

    public void setReader(String reader) {
        this.reader = reader;
    }

    public IssueDocument getIssueDocument() {
        return issueDocument;
    }

    public void setIssueDocument(IssueDocument issueDocument) {
        this.issueDocument = issueDocument;
    }



}

IssueDocument Class

@Entity
@Component
@Table(name = "Document")
public class IssueDocument implements Serializable {

    private static final long serialVersionUID = 1L;



    public IssueDocument() {
    }


    @Id
    @SequenceGenerator(name="SEQUENCE_FOR_ISSUE_DOCUMENT", sequenceName="SEQUENCE_FOR_ISSUE_DOCUMENT", allocationSize = 1)
    @GeneratedValue(strategy=GenerationType.AUTO, generator="SEQUENCE_FOR_ISSUE_DOCUMENT" )
    @Column(name="documentid")
    private Integer documentId;


    @OneToOne
    @JoinColumn(name = "issueId")
    private Issuev1 issue;

    @OneToOne
    @JoinColumn(name="username")
    private UserBaseV1 user;


    @Column(name = "documentname")
    private String documentName;
    @Column(name="updated_date")
    private LocalDateTime updatedDate;
    @Column(name="created_date")
    private LocalDateTime createdDate;
    private Boolean flagged;


    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public Boolean getFlagged() {
        return flagged;
    }

    public void setFlagged(Boolean flagged) {
        this.flagged = flagged;
    }



    public LocalDateTime getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(LocalDateTime updatedDate) {
        this.updatedDate = updatedDate;
    }



    public Integer getDocumentId() {
        return documentId;
    }

    public void setDocumentId(Integer documentId) {
        this.documentId = documentId;
    }


    public String getDocumentName() {
        return documentName;
    }

    public void setDocumentName(String documentname) {
        this.documentName = documentname;
    }




    public Issuev1 getIssue() {
        return issue;
    }

    public void setIssue(Issuev1 issue) {
        this.issue = issue;
    }



    public UserBaseV1 getUser() {
        return user;
    }

    public void setUser(UserBaseV1 user) {
        this.user = user;
    }
}

Postman Request

{

    "filename":"testing",
    "key":14,
"issueDocument":{
        "documentName":"testing"
    }

}

Upvotes: 0

Views: 93

Answers (3)

Sezer Demir
Sezer Demir

Reputation: 98

@PostMapping(path="issue-doc-controller")
public ResponseEntity saveDoc(@RequestBody IssueRequiredDetails 
issueRequiredDetails){

    return ResponseEntity.ok().build();
}

This code fragment returns an empty body with HTTP 200 OK status as the return value.

Upvotes: 0

Prabhakar Naik
Prabhakar Naik

Reputation: 1

To generate default constructor as well all argument constructor. Make sure do write logs in the execution process.

two way.

  1. to add lombok dependency
  2. manual generation

public IssueRequiredDetails(parameters, ..., ..., ...){ initialize here. }

Upvotes: 0

lohny
lohny

Reputation: 104

change..

public class IssueDocumentDTO {
    private String documentName;

    // neccesary another field
    // getter, non-args-constructor
}
public class IssueRequiredDetails implements Serializable {

    private IssueDocumentDTO issueDocument;
    // another field...
}

In my opinion, the reason to use Dto is that must not put entity object to dto class.
I think, In Object with annotation about JPA, Serialization doesn't seem to be working as expected.

Upvotes: 1

Related Questions