spring 我的请求无法接受Map数据对象并抛出400错误请求

ubbxdtey  于 2023-08-02  发布在  Spring
关注(0)|答案(2)|浏览(155)

我的Map<String, Object>参数抛出一个400 bad request error,并显示以下消息Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'data',我不知道为什么?
在我的postman中,我先到Body,然后到form-data,然后把data放在Key下面,然后把下面的值放在{ "key": "value" }下面。
带有请求参数和控制器的模型:

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);
   }

}

字符串
我的所有其他参数都可以通过,除了Map

eqqqjvef

eqqqjvef1#

此问题与Java_Spring: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'paramsMap'类似
也许可以尝试通过更改varresponse = service.send(model);
为了更接近JSON格式,类似于:{“paramsMap”:{“Key1”:“Value1”,“Key2”:“Value2”} }:

dgsult0t

dgsult0t2#

尝试使用@RequestPart注解:

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

字符串
如果你可以使用json:

@PostMapping(value = "/send", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<Response> send(@RequestBody RequestModelDto data){}

相关问题