简体   繁体   中英

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". 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<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?

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. 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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM