【发布时间】:2021-01-04 19:45:43
【问题描述】:
我正在尝试向另一个接受内容类型“application/json-patch+json”的 API 发出补丁休息请求。我正在使用 Spring 的 webclient,但我无法让它工作。我不断收到“415 不支持的媒体类型”
我已经尝试过以下方法;
WebClient webClient = WebClient.create(baseUrl);
Mono<ClientResponse> response = webClient.patch()
.uri(updateVmfExecutionApi, uuid)
.header("Content-Type", "application/json-patch+json")
.body(BodyInserters.fromFormData("lastKnownState", state))
.exchange();
我也试过了:
WebClient webClient = WebClient.create(baseUrl);
Mono<ClientResponse> response = webClient.patch()
.uri(updateVmfExecutionApi, uuid)
.contentType(MediaType.valueOf("application/json-patch+json"))
.body(BodyInserters.fromFormData("lastKnownState", state))
.exchange();
对于这两种情况,我都看到以下错误;
{"timestamp":"2020-09-17T20:50:40.818+0000","status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Unsupported Media Type","trace":"org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported\n\tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:215)\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:421)\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:367)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:449)
似乎更改为 'application/x-www-form-urlencoded;charset=UTF-8' 这种内容类型甚至可以使用 webclient 吗?
【问题讨论】:
-
您确定您的 API 接受该 Content-Type 吗?您是否尝试过在您的应用程序(即 Postman)之外对其进行测试?
-
是的,我确信它接受该内容类型。我尝试使用 swagger ui 进行测试,它可以工作。旁注:这不是我的 API,实际上是在尝试为其他服务调用 API
-
它会因您发送的正文而改变,您声明正文将包含 json,然后将表单数据粘贴到正文中,尝试更改为
.bodyValue(state) -
@ThomasAndolf 解决了这个问题。非常感谢
标签: java spring spring-boot spring-webflux spring-webclient