基于关于"' application /x-www-form-urlencoded‘不使用@RequestBody“主题的多线程,我没有成功地为WebFlux Spring应用程序找到解决方案。
我有一个WebFlux控制器方法:
@PostMapping(value = "/endpoint", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public Mono<AddRecipientResponse> createRecipient(@RequestBody
final MultiValueMap<String, String> formData,
@RequestHeader
final Map<String, String> headers) {
但是当我发布我的报告时,我得到了这个错误:
Could not resolve parameter [0] ... RecipientController.createRecipient(long,org.springframework.util.MultiValueMap<java.lang.String, java.lang.String>,java.util.Map<java.lang.String, java.lang.String>): 415 UNSUPPORTED_MEDIA_TYPE
大多数解决方案都依赖于用@RequestBody
替换@RequestParam
。然而,这不适用于我的情况。我需要@RequestBody
和consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
。我尝试了下面的线程中描述的步骤,但这些步骤都是针对Spring的。
https://stackoverflow.com/a/70148328/18734587
https://stackoverflow.com/a/51160620/18734587
有人知道如何解决使用MediaType.APPLICATION_FORM_URLENCODED
和@RequestBody
请求WebFlux应用程序的415个WebFlux错误吗?
发布于 2022-04-08 10:48:33
我自己想出了一个解决方案。请求体从ServerWebExchange.getFormData()中提取,如下所示:
@PostMapping(value = "/endpoint", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public Mono<AddRecipientResponse> createRecipient(@RequestHeader final Map<String, String> headers,
ServerWebExchange serverWebExchange) {
return serverWebExchange.getFormData()
.flatMap(formData -> recipientService.createRecipient(headers, formData));
}
https://stackoverflow.com/questions/71779603
复制相似问题