title: Spring web client return exception "Content type 'application/json' not supported for bodyType=[responseObject]
body: <p>I use web client to post a third-party API, the third-part API return response like following:</p>
  <pre><code>{
      &quot;RetailTransactionGenericResponse&quot;: {
          &quot;authID&quot;: 1146185,
          &quot;product&quot;: {
              &quot;amount&quot;: 20,
              &quot;balance&quot;: 0,
              &quot;status&quot;: &quot;D&quot;,
              &quot;metafields&quot;: {
                  &quot;metafield&quot;: [
                      {
                          &quot;name&quot;: &quot;serialNum&quot;,
                          &quot;value&quot;: 8095843490
                      }
                  ]
              }
          },
          &quot;responseCode&quot;: 0,
          &quot;responseText&quot;: &quot;Success&quot;,
          &quot;transactionID&quot;: &quot;b0924cca-f2a9-477f-915b-9153e74ebce0&quot;
      }
  }
  </code></pre>
  <p>So In my <code>payload</code> package, it have <code>RetailTransactionGenericResponseWrapper</code> class, which contains <code>RetailTransactionGenericResponse</code> class.</p>
  <p>Here is the code for <code>RetailTransactionGenericResponse</code> class</p>
  <pre><code>import com.fasterxml.jackson.annotation.JsonCreator;
  import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.annotation.JsonProperty;
  import lombok.AllArgsConstructor;
  import lombok.Builder;
  import lombok.Data;
  import lombok.ToString;
  
  @Data
  @ToString
  @Builder
  @AllArgsConstructor(onConstructor_ = @JsonCreator)
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public class RetailTransactionGenericResponse {
  
      @JsonProperty
      private String authID;
  
      @JsonProperty
      private Product product;
  
      @JsonProperty
      private String responseCode;
  
      @JsonProperty
      private String responseText;
  
      @JsonProperty
      private String transactionID;
  }
  </code></pre>
  <p>For <code>Product</code> Class</p>
  <pre><code>import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.annotation.JsonProperty;
  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
  import lombok.AllArgsConstructor;
  import lombok.Builder;
  import lombok.Data;
  import lombok.NoArgsConstructor;
  import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
  import org.apache.commons.lang3.builder.ToStringStyle;
  
  @Data
  @Builder
  @AllArgsConstructor
  @NoArgsConstructor
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public class Product {
  
      @JsonProperty
      @JsonInclude(JsonInclude.Include.NON_NULL)
      private double amount;
  
      @JsonProperty
      @JsonInclude(JsonInclude.Include.NON_NULL)
      private String code;
  
      @JsonProperty
      @JsonInclude(JsonInclude.Include.NON_NULL)
      private String upc;
  
      // Response part
  
      @JsonProperty
      @JsonInclude(JsonInclude.Include.NON_NULL)
      private MetaFields metafields;
  
      @JsonProperty
      @JsonInclude(JsonInclude.Include.NON_NULL)
      private String balance;
  
      @JsonProperty
      @JsonInclude(JsonInclude.Include.NON_NULL)
      private String status;
  
      @Override
      public String toString() {
          return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE);
      }
  }
  </code></pre>
  <p>And For <code>MetaFields</code> class</p>
  <pre><code>import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.annotation.JsonProperty;
  import lombok.Data;
  
  import java.util.List;
  
  @Data
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public class MetaFields {
  
      @JsonProperty
      private List&lt;MetaField&gt; metafield;
  }
  </code></pre>
  <p>And <code>MetaField</code> Class</p>
  <pre><code>import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.annotation.JsonProperty;
  import lombok.Data;
  
  @Data
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public class MetaField {
  
      @JsonProperty
      private String name;
  
      @JsonProperty
      private String value;
  }
  </code></pre>
  <p>So in my service here is how I try to retrieve the response back</p>
  <pre><code>RetailTransactionGenericResponseWrapper response =
      webClient.post().uri(url).headers(headers -&gt; headers.setBearerAuth(token)).accept(APPLICATION_JSON)
          .body(Mono.just(retailTransactionGenericRequest), RetailTransactionGenericResponseWrapper.class)
          .retrieve().bodyToMono(RetailTransactionGenericResponseWrapper.class).block();
  </code></pre>
  <p>What do I do wrong that it cannot map the json to object?</p>
  <p>And here is the exception stack</p>
  <pre><code>ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.reactive.function.client.WebClientResponseException: 200 OK from POST https://[third_party_url]; nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType=com.xxx.xxx.payload.xxx.RetailTransactionGenericResponseWrapper] with root cause
  org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType=com.xxx.xxx.payload.xxx.RetailTransactionGenericResponseWrapper
      at org.springframework.web.reactive.function.BodyExtractors.lambda$readWithMessageReaders$12(BodyExtractors.java:201)
      Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
  Error has been observed at the following site(s):
      |_ checkpoint ⇢ Body from POST https://[third_party_url] [DefaultClientResponse]
  Stack trace:
          at org.springframework.web.reactive.function.BodyExtractors.lambda$readWithMessageReaders$12(BodyExtractors.java:201)
          at java.base/java.util.Optional.orElseGet(Optional.java:369)
          at org.springframework.web.reactive.function.BodyExtractors.readWithMessageReaders(BodyExtractors.java:197)
          at org.springframework.web.reactive.function.BodyExtractors.lambda$toMono$2(BodyExtractors.java:85)
          at org.springframework.web.reactive.function.client.DefaultClientResponse.body(DefaultClientResponse.java:132)
          at org.springframework.web.reactive.function.client.DefaultClientResponse.bodyToMono(DefaultClientResponse.java:147)
          at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultResponseSpec.lambda$bodyToMono$2(DefaultWebClient.java:541)
          at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:125)
          at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:73)
          at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:120)
          at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
          at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:199)
          at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:199)
          at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:199)
          at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:82)
          at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2346)
          at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onSubscribeInner(MonoFlatMapMany.java:150)
          at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onNext(MonoFlatMapMany.java:189)
          at reactor.core.publisher.SerializedSubscriber.onNext(SerializedSubscriber.java:99)
          at reactor.core.publisher.FluxRetryWhen$RetryWhenMainSubscriber.onNext(FluxRetryWhen.java:173)
          at reactor.core.publisher.MonoCreate$DefaultMonoSink.success(MonoCreate.java:160)
          at reactor.netty.http.client.HttpClientConnect$HttpIOHandlerObserver.onStateChange(HttpClientConnect.java:389)
          at reactor.netty.ReactorNetty$CompositeConnectionObserver.onStateChange(ReactorNetty.java:612)
          at reactor.netty.resources.DefaultPooledConnectionProvider$DisposableAcquire.onStateChange(DefaultPooledConnectionProvider.java:195)
          at reactor.netty.resources.DefaultPooledConnectionProvider$PooledConnection.onStateChange(DefaultPooledConnectionProvider.java:466)
          at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:613)
          at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:94)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
          at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
          at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:311)
          at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:432)
          at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
          at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1533)
          at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1282)
          at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1329)
          at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508)
          at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:447)
          at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
          at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
          at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
          at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
          at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
          at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
          at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
          at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
          at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
          at java.base/java.lang.Thread.run(Thread.java:834)
  </code></pre>
  <p>Based on Nikolai's comment I tried following...</p>
  <pre><code>RetailTransactionGenericResponseWrapper response =
      webClient.post().uri(url).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
          .headers(headers -&gt; {
              headers.setBearerAuth(token);
          }).accept(APPLICATION_JSON)
          .body(Mono.just(retailTransactionGenericRequest), RetailTransactionGenericResponseWrapper.class)
          .retrieve().bodyToMono(RetailTransactionGenericResponseWrapper.class).block();
  </code></pre>
  <p>And like this</p>
  <pre><code>RetailTransactionGenericResponseWrapper response = webClient.post().uri(url).headers(headers -&gt; {
      headers.setBearerAuth(token);
      headers.setContentType(APPLICATION_JSON);
  }).contentType(APPLICATION_JSON).accept(APPLICATION_JSON)
      .body(Mono.just(retailTransactionGenericRequest), RetailTransactionGenericResponseWrapper.class).retrieve()
      .bodyToMono(RetailTransactionGenericResponseWrapper.class).block();
  </code></pre>
  <p>I am still get the same exception stack</p>