0

I have a payload that am trying to save into a database, whenever I post the payload, it returns the mapped JSON objects as null instead of the values in the payload passed.

here is the JSON payload

{
    "authStatus": {
        "authStatusCode": 131,
        "authStatusDescription": "API call doesn't need authentication"
    },
    "results": {
        "beepTransactionID": 10764659,
        "payerTransactionID": "5f51eed7347a7",
        "statusCode": "188",
        "statusDescription": "Response was received"
    }
}

after posting it to postman

{
    "id": 14,
    "authStatusCode": null,
    "authStatusDescription": null,
    "beepTransactionID": null,
    "payerTransactionID": null,
    "statusCode": null,
    "statusDescription": null
}

my controller for posting

     @PostMapping("/payload")
        public Payload createPayload(@Valid @RequestBody Payload payload) {
            return payloadRepository.save(payload);
        }

my model

@Entity
@Table(name = "payload", schema = "public")
    public class Payload {
        private long id;
        Integer authStatusCode;
        String authStatusDescription;
        Integer beepTransactionID;
        String payerTransactionID;
        String statusCode;
        String statusDescription;
    
        public Payload(){
    
        }
    
    //getters and setters
2
  • Are you having three tables one for payload, one for Authstatus and another for results or else its just the same Commented Feb 12, 2021 at 6:36
  • Hi @venkat just the same table Commented Feb 12, 2021 at 6:38

2 Answers 2

2

You need intermediary objects, Payload needs to be a bit deeper to match exactly the format of your JSON object

public class Payload {
    AuthStatus authStatus;
    Results results;
    // Add boilerplate code here
}

public class AuthStatus {
    Integer authStatusCode;
    String authStatusDescription;
    // Add boilerplate code here
}

public class Results {
    Integer beepTransactionID;
    String payerTransactionID;
    String statusCode;
    String statusDescription;
    // Add boilerplate code here
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi @Yassin and where do I set the getters and setters for the above and how do I call save?
Raymond, add the getters and setters yourself no problem with that. The saving mechanism remains the same.
public class AuthStatus and public class Results are complaining that I dnt make them public, if they are public they are throwing an error
Create them in their own file
Which class will have the entity annotation and table name? And which one will house the primary id?
|
0

Try using

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")

to store payload into JsonNode format as it is

1 Comment

hi @dhiraj, I want to extract the specific values from the json string and insert to columns ands rows, not the entire json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.