小编ran*_*and的帖子

如何将 Spring Webclient 的内容类型设置为“application/json-patch+json”

我正在尝试向另一个接受内容类型“application/json-patch+json”的 API 发出补丁休息请求。我正在使用 Spring 的网络客户端,但我无法让它工作。我不断收到“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();
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

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();
Run Code Online (Sandbox Code Playgroud)

对于这两种情况,我都会看到以下错误;

 {"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)
Run Code Online (Sandbox Code Playgroud)

似乎它更改为 'application/x-www-form-urlencoded;charset=UTF-8' 是否可以为此内容类型使用 webclient?

java spring spring-boot spring-webflux spring-webclient

2
推荐指数
1
解决办法
2万
查看次数