Dams
Dams

Reputation: 47

Map<String, String> as request parameter in spring boot application

I am migrating my spring boot application from 1.5.x to 2.3.x. After updating the required dependencies, in a controller having below method, getting one error:

@RequestMapping(value= "/test", method = RequestMethod.POST)
public ResponseEntity<MyResult> getTestRes(
                                       @RequestParam("test1") final String test1,
                                       @RequestParam("test2") final String test2,
                                       @RequestParam("test3") final String test3,
                                       @RequestParam Map<String, String> reqParam,
                                       final HttpServeletRequest req) throws Exception {

        //method body.....
}

When i try to build the application, i get below error:

[ERROR] Cant use non-primitive type: java.util.Map<java.lang.String, java.lang.String> as request parameter.

I am using Spring boot 2.3.9.RELEASE version with Spring framework 5.2.13.RELEASE.

Please let me know how can i resolve this issue.

Thanks

Upvotes: 0

Views: 2224

Answers (1)

Juliyanage Silva
Juliyanage Silva

Reputation: 2699

Since you are using POST, I suggest you to do something like:

@RequestMapping(value = "/deleteData",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Result getTestRes(@RequestBody HashMap<String, Object> dataHashMap) {
Object1 object1=  (Object1) JsonConvertor.jsonToObject((String) dataHashMap.get("object1"), Object1.class);
    Object2 object2= (Object2) JsonConvertor.jsonToObject((String) dataHashMap.get("object2"), Object2.class);   
}

Somthing like above should work. BUT if not you can have a wrapper object and pass that hashmap as key value string from client and then parse in in Java to a hashmap.

Upvotes: 0

Related Questions