0

I showed my snippet below where I am trying to send json obj as Reuestbody and my controller could not assign the requested value.

REQUESTED JSON OBJECT

{
    "Request":
        {
        "ReferenceNumber" : "ILT06240123201694516287",
        "B_Code" : 1,
        "B_Code":"888asdad88",
        "Request":"11111111111111111"
        }
}

Controller

@RequestMapping(value="/GetAccountDetails",method = RequestMethod.POST)
    public ResponseEntity<AccountListResponse> GetAccountDetails(@RequestBody @Valid CBSAccountRequest cbsAccountReq
            ,BindingResult result) {
        if(result.hasErrors()) {
            throw new InvalidException("Not Valid",result);
        }
        else {
            AccountListResponse accountListResponse=new AccountListResponse();
            return new ResponseEntity<AccountListResponse>(accountListResponse, HttpStatus.OK); 
        }
    }

Pojo

public class CBSAccountRequest {

    @NotNull
    @Size(min=25,max=25,message="Reference number should have 25 characters")
    private String ReferenceNumber;
    @NotNull
    @Digits(integer=1,fraction = 0 )
    private int B_Code;
    @NotNull
    @Size(min=5,max=5, message="Invalid Branch Code")
    private String B_Code;
    @NotNull
    @Size(min=17,max=17 ,message="Invalid Account Number")
    private String Request;
    //getters and setters
}

I am getting exceptions because of @Valid.I go through lot of questions related to it and none of them is working for me. I predicted that the issue may happen because of JSON object structure. I also tried with below object which also not working.

{
    "ReferenceNumber" : "ILT06240123201694516287",
    "B_Code" : 1,
    "B_Code":"888asdad88",
    "Request":"11111111111111111"
}
sunkuet02
  • 2,376
  • 1
  • 26
  • 33
Mohanraj
  • 396
  • 3
  • 8
  • 21

3 Answers3

3

It seems to me that you are sending JSON request with a wrong structure. In your JSON the outer "Request" element is redundant. Try to send the following request instead:

{
  "ReferenceNumber" : "ILT06240123201694516287",
  "B_Code" : 1,
  "B_Code":"888asdad88",
  "Request":"11111111111111111"
}

BTW, as a suggestion. You can use java naming convention for your fields and you will be still able to map names like "B_Code" to them using @JsonProperty annotation:

@JsonProperty("B_Code")
String bCode;
Danylo Zatorsky
  • 5,856
  • 2
  • 25
  • 49
0

Problem is Your JSON is INVALID

{
    "ReferenceNumber" : "ILT06240123201694516287",
    "B_Code" : 1,
    "B_Code":"888asdad88",
    "Request":"11111111111111111"
}

You have Duplicated key B_Code in your request payload.

Here is what you can do:

  1. change the name of field B_Code in json
  2. change the name of field B_Code in Java, it doesn't match the java naming convention.
  3. Make sure you don't have duplicate fields in json
  4. If the name of field is different than the name of the field in java, you need the annotation @JsonProperty to correct it.
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
0

Also consider this: the Java convention for naming a variable in a POJO demands that the first variable is in lowercase. You can however override this by using the @JsonNaming annotation. Check out these threads: https://stackoverflow.com/questions/38935912/requestbody-is-getting-null-values/38939812#38939812

1: @RequestBody is getting null values and @RequestBody is getting null values

You can also read: Jackson Property Custom Naming Strategy

This thread will also be of help: Case insensitive JSON to POJO mapping without changing the POJO

mPango
  • 1
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29903705) – Muhammedogz Sep 24 '21 at 00:49