0

There a json object to be sent to the server, which contains a field:

{"sName":"something"}

In my request model,I declare a var with the same name:

String sName;

But I got null when I receive in the Controller.

I change the field name to lower-case(sname) or add JsonProperty(value="sName") annotation,it work. So where is the problem?

Controller

public ResponseEntity<JSONObject> getComprehensiveInquiryCp(@Validated @RequestBody ComprehensiveInquiryRequestModel body) {
        Map<String, Object> content;
        JSONObject result = new JSONObject();
        String sLicense = body.getSLicense();
...
}

ComprehensiveInquiryRequestModel

@Data
public class ComprehensiveInquiryRequestModel {
...
    //@JsonProperty(value = "sLicense")
    private String sLicense;
...
}
3
  • first your json object is wrong, it should be {"sName":"something'"} Commented May 26, 2020 at 15:04
  • can you add full code Commented May 26, 2020 at 15:05
  • @Hemant updated. Commented May 26, 2020 at 15:17

2 Answers 2

1

From top of my head: if you have accessors in that bean, then I think jackson prefers to use them if they exist. And/or Jackson prefers accessors for private fields. As you noticed you can alter that behaviour with Jakson configuration (for example via annotions).

Try: 1. to debug, remove accessor methods and make field public. If that works then change the field back to private and make sure accessor methods are named correctly.

Also single charater prefixes are not a good practise. They can be problematic and confusing. Prefixes in general are lazy and un-Clean Code(tm) practise.

Sign up to request clarification or add additional context in comments.

Comments

0

It is important that your setters (and getters) are present and actually conform to the Java naming conventions. A json property named "myFirstName" usually requires a public setter "setMyFirstName(...)" for example. So "sName" needs "setSName()", I guess. Sure sounds like the naming convnetion might be at fault here.

Comments

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.