0

My Map<String, Object> parameter is throwing a 400 bad request error with the following message, Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'data' and I am not sure why?

In my postman, I go to Body then form-data and I place data under Key then I place the following value { "key": "value" }.

My model with the request parameter and controller:

public class RequestModel {

   @Schema(type = "object")
   private Map<String, Object> data;

   private MultipartFile file;
   private String requestId;
   private String date;

}

public class Controller {
   
   @PostMapping(value = "/send", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {
        MediaType.MULTIPART_FORM_DATA_VALUE })
   public ResponseEntity<Response> send(@ModelAttribute final RequestModel model) {
      var response = service.send(model);
      return ResponseEntity.ok(response);
   }

}

All my other parameters is able to get pass fine, except for the Map.

2 Answers2

0

This question seems similar to Java_Spring: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'paramsMap'

Perhaps try changing how you're sending the body by changing var response = service.send(model);

To more closely resemble the json format something like: { "paramsMap": { "Key1":"Value1", "Key2":"Value2" } }:

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '23 at 07:14
0

Try with @RequestPart annotation :

@PostMapping(value = "/send", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE })
public ResponseEntity<Response> send(@RequestPart Map<String, Object> data){}

Or if you can consume json then :

@PostMapping(value = "/send", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<Response> send(@RequestBody RequestModelDto data){}
elgsylvain85
  • 421
  • 4
  • 10
  • I can't use RequestPart because I need to send a file within the api call – user21476047 Jul 14 '23 at 14:59
  • "@RequestPart("file") MultipartFile file" / "@RequestPart("files") List files" or cast item from your "Map data". You can also debug your request to explore "data" – elgsylvain85 Jul 14 '23 at 15:05