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

huangapple go评论80阅读模式
英文:

How do I set Content type for Spring Webclient to "application/json-patch+json"

问题

我正在尝试向另一个接受内容类型为 "application/json-patch+json" 的 API 发送补丁 REST 请求。我正在使用 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?

英文:

I am trying to make a patch rest request to another API that accepts content type "application/json-patch+json". I am using Spring's webclient but I have not been able to get it to work. I keep getting "415 Unsupported media type"

I have tried the below;

WebClient webClient = WebClient.create(baseUrl);
Mono&lt;ClientResponse&gt; response = webClient.patch()
  .uri(updateVmfExecutionApi, uuid)
  .header(&quot;Content-Type&quot;, &quot;application/json-patch+json&quot;)
  .body(BodyInserters.fromFormData(&quot;lastKnownState&quot;, state))
  .exchange();

I also tried:

WebClient webClient = WebClient.create(baseUrl);
    Mono&lt;ClientResponse&gt; response = webClient.patch()
      .uri(updateVmfExecutionApi, uuid)
      .contentType(MediaType.valueOf(&quot;application/json-patch+json&quot;))
      .body(BodyInserters.fromFormData(&quot;lastKnownState&quot;, state))
      .exchange();

For both cases I see the following error;

 {&quot;timestamp&quot;:&quot;2020-09-17T20:50:40.818+0000&quot;,&quot;status&quot;:415,&quot;error&quot;:&quot;Unsupported Media Type&quot;,&quot;exception&quot;:&quot;org.springframework.web.HttpMediaTypeNotSupportedException&quot;,&quot;message&quot;:&quot;Unsupported Media Type&quot;,&quot;trace&quot;:&quot;org.springframework.web.HttpMediaTypeNotSupportedException: Content type &#39;application/x-www-form-urlencoded;charset=UTF-8&#39; 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)

Seems it changes to 'application/x-www-form-urlencoded;charset=UTF-8'
Is it even possible to use webclient for this content-type?

答案1

得分: 4

如果您查看异常,您会看到它说

不支持内容类型 &#39;application/x-www-form-urlencoded;charset=UTF-8&#39;

它已将其更改为 formdata。这是因为实际在请求体中发送的内容具有优先级。在您的代码中,您正在声明以下内容以发送请求体。

.body(BodyInserters.fromFormData(&quot;lastKnownState&quot;, state))

这表明您正在发送表单数据,这是一种以键值方式发送数据的方法,webclient 将自动为您设置内容类型标头为 x-www-form-urlencoded

如果您想要具有 JSON 内容类型标头,则需要发送 JSON 数据。发送 JSON 是 webclient 的默认方式,因此您只需要正确传递请求体。有几种通过标准方式传递请求体的方法。

可以通过传递生成器(可以是 MonoFlux)之一来实现,

.body(Mono.just(data))

使用 BodyInserter#fromValue

.body(BodyInserters.fromValue(data))

或者使用前面方法的简写(也是最简单的方法)

.bodyValue(data)
英文:

If you look into the exception you can see it says

Content type &#39;application/x-www-form-urlencoded;charset=UTF-8&#39; not supported

It changed it to formdata. Thats because what you actually send in the body has priority. In your code you are stating the following to send the body.

.body(BodyInserters.fromFormData(&quot;lastKnownState&quot;, state))

This states that you are sending form data, which is a key value way of sending data, and webclient will then set the content type header for you automatically to x-www-form-urlencoded.

You need to send json data if you want to have a json content type header. Sending json is the default way for webclient so all you need to do is to pass in the body correctly. There are several ways of passing the body in the standard way.

either by passing a producer (could be a Mono or a Flux).

.body(Mono.just(data))

Using BodyInserter#fromValue.

.body(BodyInserters.fromValue(data))

or the shorthand for the previous one (which is the simplest)

.bodyValue(data)

huangapple
  • 本文由 发表于 2020年9月18日 04:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63946105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定