问题
The error show up when My code like this:
@Test
public void getTemplateByIdTest() throws Exception {
client.get().uri("/template/getTemplate/7")
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody(VtTemplateVO.class)
.returnResult();
}
When I change my code like this,it's ok!
@Test
public void getTemplateByIdTest() throws Exception {
client.get().uri("/template/getTemplate/7")
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody(String.class)
.returnResult();
}
Why when I use .expectBody(VtTemplateVO.class)
it will say org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=UTF-8' not supported
Somebody knows? please help,thanks
回答1:
I was also facing this issue caused by Jackson.There needs to be a default constructor for the VtTemplateVO.class with annotated properties. Ex. what I did-
@JsonCreator
public Comment(@JsonProperty("id")String id, @JsonProperty("text")String text, @JsonProperty("createdTime") LocalDate createdTime) {
this.id = id;
this.text = text;
this.createdTime = createdTime;
}
Hope it works for you as well. :)
回答2:
Instead of annotating every parameter with @JsonProperty
, you can also compile your code with the -parameters
flag (since Java 8). This will retain the parameter names in the bytecode thus allowing Jackson to use them. See the Maven docs
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
You should also consider this https://stackoverflow.com/a/44075684/2145769 that now parameter names become part of your API.
回答3:
Use
.accept(MediaType.APPLICATION_JSON) as
webClientBuilder.build() .post() .uri(ccpApiUrl) //.accept(MediaType.APPLICATION_JSON) .bodyValue(customerRequest) .exchange() .doOnSuccess( clientResponse -> logger.info("clientResponse.headers() = " + clientResponse.headers()) ) .doOnSuccess( clientResponse -> { logger.info("clientResponse.statusCode() = " + clientResponse.statusCode()); getStatus(clientResponse.statusCode());
})
//.timeout(Duration.ofSeconds(5))
.flatMap(clientResponse -> clientResponse.bodyToMono(CcpResponse.class))
// .retryWhen(fixedRetry)
.block();
来源:https://stackoverflow.com/questions/51514296/org-springframework-web-reactive-function-unsupportedmediatypeexception-content