Home:ALL Converter>Accessing dynamically added properties in Spring @RequestBody

Accessing dynamically added properties in Spring @RequestBody

Ask Time:2019-10-23T15:02:50         Author:seokhoonlee

Json Formatter

I have a post mapping like below:

@PostMapping(value = "/profiles/{profileId}/verify/")
public Response getVerificationInformation (
    @RequestBody VerificationBody body) {
    ... do something with the body
    ... call function A with body object
}

And later on at function A, I am accessing the body object for a few attributes.

Also, in the front-end, I am modifying the JSON object (I am adding another property) that hits this post mapping.

For example,

{
    "name" : "Example",
    "profileId" : "123",
    // and I am dynamically adding an attribute 'country'
    "country" : "US"
}

The problem is at function A, I have no means to get information about the dynamically added attribute (in this case 'country').

Declaring a getter for dynamically added properties is not ideal because there are many properties being dynamically added.

I have tried something in the direction of @JsonAnySetter and @JsonAnyGetter but I am getting 400. And I am looking for other solutions.

Please help and thank you in advance! (I simplified a few names of variables and functions but I hope it's not too hard to understand).


VerificationBody can be thought of as below:

public class VerificationBody {
    @JsonProperty(value = "name")
    String name,
    @JsonProperty(value = "profileId")
    Long profileId,
    // ... it does not include country
}

Managed to fix the 400 issue and I am able to get the properties through JsonAnyGetter and JsonAnySetter.

Author:seokhoonlee,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58517178/accessing-dynamically-added-properties-in-spring-requestbody
Vinay Prajapati :

Your VerificationBody class could be something like below:\n\nclass VerificationBody {\nprivate String prop1;\n//other properties & their getters and setter\n\nprivate Map<String, ? extends Object> otherProps;\n// getter setters for otherProps\n\n}\n\n\nThis will allow you to always receive additional properties without any issues with extension.",
2019-10-23T07:07:44
Abhishek :

You can use HashMap something like this to tackle this kind of problem :\n\n@RequestMapping(value = \"/profiles/{profileId}/verify/\", headers = \"Accept=application/json\", method = RequestMethod.POST)\npublic void verifyBody(@RequestBody HashMap<String, HashMap<String, String>> requestData) {\n\nHashMap<String, String> customerInfo = requestData.get(\"verificationBody\");\nString param1 = customerInfo.get(\"param1\");\n//TODO now do whatever you want to do.\n}\n",
2019-10-23T07:09:16
Amritha Vibhuth C Hitige :

The annotation for the request body is @RequestBody. As the request body is a key-value pair, it will be wise to declare it as a Map.\n\n@PostMapping(\"/blog\")\npublic Blog create(@RequestBody Map<String, String> body){...}\n\n\nTo extract the respective keys and their values:\n\nString id = body.get(\"id\");\nString title = body.get(\"title\");\nString content = body.get(\"content\");\n\n\nTry with this link\n\nhttps://medium.com/better-programming/building-a-spring-boot-rest-api-part-ii-7ff1e4384b0b",
2019-10-23T07:11:27
LHCHIN :

The root cause is your JSON string is invalid, a valid one is supposed to look like below:\n\n{\n \"name\": \"Example\",\n \"profileId\": \"123\",\n \"country\": \"US\"\n}\n\n\nPlease ensure that every key is double-quoted, or you will get 400 Bad Request while serializing the request body to your POJO by using Jackson.\n\nBTW, I'm using Spring Boot and I can reproduce getting HTTP status code 400 by your code snippet with the invalid JSON string as payload.",
2019-10-23T08:01:11
yy