POST:@ModelAttribute 不转换包装器 DTO 的 Map 对象

问题描述 投票:0回答:1

我需要你的帮助来解决一个 java Map 对象的 @ModelAttribute 问题。

上下文

以下 REST 控制器公开了一个接受包装器 dto 的 POST api。

@Validated
@RestController
@Slf4j
@RequestMapping(path = "/api/v1/modelattribute", produces = MediaType.APPLICATION_JSON_VALUE)
public class AController {

  @PostMapping(value = "wrapperdto", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
  public void testMapInWrapperDto(@ModelAttribute("wrapper") WrapperDto wrapper) {
    log.warn(wrapper.toString());
  }
@NoArgsConstructor
@AllArgsConstructor
@Data
public class WrapperDto {
  private String number;
  private Map<String, String> map = new HashMap<>();
}

问题

当我通过 swagger UI 或通过 curl 命令调用此 api 时,spring 响应以下错误

2023-03-31 09:01:55,666 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping         : Mapped to fr.gregoire.passpro.backend.aaaa.AController#testMapInWrapperDto(WrapperDto)
2023-03-31 09:01:55,666 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet  : POST "/api/v1/modelattribute/wrapperdto", parameters={multipart}
2023-03-31 09:01:55,668 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping         : Mapped to fr.gregoire.passpro.backend.aaaa.AController#testMapInWrapperDto(WrapperDto)
2023-03-31 09:01:55,668 [http-nio-8080-exec-10] DEBUG org.springframework.web.method.HandlerMethod       : Could not resolve parameter [0] in public void fr.gregoire.passpro.backend.aaaa.AController.testMapInWrapperDto(fr.gregoire.passpro.backend.aaaa.WrapperDto): org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'wrapper' on field 'map': rejected value [{
  "cheese-burger": "280g",
  "classic-burger": "50g"
}]; codes [typeMismatch.wrapper.map,typeMismatch.map,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [wrapper.map,map]; arguments []; default message [map]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'map': no matching editors or conversion strategy found]
2023-03-31 09:01:55,669 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver    : Using @ExceptionHandler fr.gregoire.passpro.backend.resterror.RestExceptionHandler#handleException(Exception, WebRequest)
2023-03-31 09:01:55,669 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.annotation.HttpEntityMethodProcessor   : Using 'application/octet-stream', given [*/*] and supported [*/*]
2023-03-31 09:01:55,669 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver    : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors<EOL>Field error in object 'wrapper' on field 'map': rejected value [{<EOL><EOL>  "cheese-burger": "280g",<EOL><EOL>  "classic-burger": "50g"<EOL><EOL>}]; codes [typeMismatch.wrapper.map,typeMismatch.map,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [wrapper.map,map]; arguments []; default message [map]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'map': no matching editors or conversion strategy found]]
2023-03-31 09:01:55,669 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet  : Completed 400 BAD_REQUEST

执行的curl命令:

curl -X 'POST' \
  'http://localhost:8080/api/v1/modelattribute/wrapperdto' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'number=65431' \
  -F 'map={
  "cheese-burger": "280g",
  "classic-burger": "50g"
}'

框架

  • 弹簧靴:2.7.7
  • springdoc-openapi-ui : 1.6.14 (=swagger-ui)
  • vuejs:2.6.14
  • 卷曲命令
  • 龙目岛

问题

如何将 Map 对象传递给此 POST api?

感谢您的帮助
尼古拉斯

java spring-boot rest swagger-ui modelattribute
1个回答
0
投票

我尝试在 curl 命令中将地图设置在一行中

curl -X 'POST' \
  'http://localhost:8080/api/v1/modelattribute/wrapperdto' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'number=sdfg' \
  -F 'map={"cheese-burger":"280g","classic-burger":"50g"}'

我也尝试使用 Map 而不是 Map 和 sme 样式错误:

@NoArgsConstructor
@AllArgsConstructor
@Data
public class WrapperDto {
  private String number;
  private Map<Object, Object> map;
}
© www.soinside.com 2019 - 2024. All rights reserved.