简体   繁体   English

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

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

I am trying to make a patch rest request to another API that accepts content type "application/json-patch+json".我正在尝试向另一个接受内容类型“application/json-patch+json”的 API 发出补丁休息请求。 I am using Spring's webclient but I have not been able to get it to work.我正在使用 Spring 的 webclient,但我无法让它工作。 I keep getting "415 Unsupported media type"我不断收到“415 Unsupported media type”

I have tried the below;我试过以下;

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();

I also tried:我也试过:

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();

For both cases I see the following error;对于这两种情况,我看到以下错误;

 {"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)

Seems it changes to 'application/x-www-form-urlencoded;charset=UTF-8' Is it even possible to use webclient for this content-type?似乎它更改为 'application/x-www-form-urlencoded;charset=UTF-8' 是否可以将 webclient 用于此内容类型?

If you look into the exception you can see it says如果您查看异常,您可以看到它说

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

It changed it to formdata.它把它改成了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("lastKnownState", 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 .这表明您正在发送表单数据,这是发送数据的键值方式,然后 webclient 会自动为您设置内容类型标头为x-www-form-urlencoded

You need to send json data if you want to have a json content type header.如果你想要一个 json 内容类型头,你需要发送 json 数据。 Sending json is the default way for webclient so all you need to do is to pass in the body correctly.发送 json 是 webclient 的默认方式,因此您需要做的就是正确传递正文。 There are several ways of passing the body in the standard way.有几种方法可以以标准方式传递身体。

either by passing a producer (could be a Mono or a Flux ).通过传递生产者(可以是MonoFlux )。

.body(Mono.just(data))

Using BodyInserter#fromValue .使用BodyInserter#fromValue

.body(BodyInserters.fromValue(data))

or the shorthand for the previous one (which is the simplest)或前一个的简写(这是最简单的)

.bodyValue(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 contentType application/json 而不是 application/json-patch+json 的 PATCH - PATCH with contentType application/json instead of application/json-patch+json Spring WebClient put Mapping:不支持内容类型&#39;application / json&#39; - Spring WebClient put Mapping: Content type 'application/json' not supported 索引大于9的集合项上的Spring Data Rest json-patch + json - Spring Data Rest json-patch+json on collection item with index greater than 9 我如何设置 Content-Type 以响应 application/json - How I can set Content-Type in react to application/json Spring MVC 4:“application/json”内容类型设置不正确 - Spring MVC 4: “application/json” Content Type is not being set correctly Spring MVC - 不支持内容类型“应用程序/json” - Spring MVC - Content type 'application/json' not supported 在 Spring 中读取 Content-Type 应用程序/json - Reading Content-Type application/json in Spring 当我尝试将 JSON 发送到 Spring 时,不支持内容类型“application/json;charset=UTF-8” - Content type 'application/json;charset=UTF-8' not supported, when i try send JSON to Spring 如何在Spring Data REST中使用JSON补丁? - How do you use JSON Patch with Spring Data REST? 为什么日期转换在 Spring 中对内容类型为 json-patch 的请求不起作用? - Why won't the date conversion work in Spring on requests with content-type json-patch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM