Ad

Mapping Json In Body To Model Issue Spring Boot

- 1 answer

New to spring boot.

Trying to get the json from the request body in the RestController.

I am passing the json input as

{
    "something":"54545",
    "intgervalue":"1212"
}

I am having the model as

public class TempClass {

    private String something_1;

    private int intgervalue_1;

    //With Getters and Setters
}

I am exposing Rest API as,

@RequestMapping(value = "/<PathGoesHere>", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
@CrossOrigin(origins = "*")
public WidgetsResponse getdashboardwidgets(@RequestBody TempClass entity) {

}

I've studied like, this json to model conversion is taken care by Jackson on behalf of Spring if the Jackson is in the classpath.

But Jackson is mapping the input json to the model even though the names are completely different.

I am seeing entity object is having value as, something_1 with "54545" and intgervalue_1 with "1212".

I even tried with annotation, @JsonProperty("TargetNameHere") on the POJO props. But still no luck.

I am expecting like, if the names in the Json and in the Model matches, then only Jackson has to map it else should not map.

Could someone give some input on this one?

Ad

Answer

Jackson can auto-detect properties from public getters/setters.

In your case I assume that your setter is named setSomething() thus jackson maps the value using the set method. If you do not that then the setter is to be renamed.

Ad
source: stackoverflow.com
Ad