当我的代码如下时出现错误:
@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();
}
当我像这样更改代码时,就可以了!
@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();
}
为什么当我使用
.expectBody(VtTemplateVO.class)
时它会说org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=UTF-8' not supported
有人知道吗?请帮忙,谢谢
我也面临着 Jackson 引起的这个问题。VtTemplateVO.class 需要有一个带有注释属性的默认构造函数。前任。我做了什么-
@JsonCreator
public Comment(@JsonProperty("id")String id, @JsonProperty("text")String text, @JsonProperty("createdTime") LocalDate createdTime) {
this.id = id;
this.text = text;
this.createdTime = createdTime;
}
希望它也适合您。 :)
显然,创建和填充对象时的任何问题都可能导致此错误。
就我而言,我重复了
@JsonProperty
,例如:
public class MyObject {
@JsonProperty("foo")
private String foo;
@JsonProperty("foo")
private String bar;
}
所以我固定为:
public class MyObject {
@JsonProperty("foo")
private String foo;
@JsonProperty("bar")
private String bar;
}
并且成功了。
但是我看到其他人评论说其他错误可能会导致此错误,例如:
您还可以使用
@JsonProperty
标志编译代码(自 Java 8 起),而不是使用 -parameters
注释每个参数。这将保留字节码中的参数名称,从而允许杰克逊使用它们。请参阅 Maven 文档
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
您还应该考虑这一点 https://stackoverflow.com/a/44075684/2145769 现在参数名称已成为您 API 的一部分。
对我来说,这个问题是由重复的 @Jsonproperty 值引起的,正如 Jefferson Lima 在他的回答中指出的那样。我在您的问题中看不到您的 VtTemplateVO POJO 类,但这可能会导致问题。
就我而言,当发生此异常时,我尝试从 POJO 类中的“游戏”JSON 节点中解压多个嵌套值,以获取所需的映射模式。
"game": {
"home_team_id": 2
"visitor_team_id": 5
}
以下方法不起作用。
public class MyPojo {
@JsonProperty("game")
public void unpackHomeTeamId(Map<String, String> game) {
homeTeamId = game.get("home_team_id");
}
@JsonProperty("game")
public void unpackAwayTeamId(Map<String, String> game) {
awayTeamId = game.get("visitor_team_id");
}
}
解决方案是在一种方法中设置所有字段,从而避免混淆杰克逊映射器。这也让我有了更简洁的设计。
@JsonProperty("game")
public void unpackNestedGameProperties(Map<String, String> game) {
homeTeamId = game.get("home_team_id");
awayTeamId = game.get("visitor_team_id");
}
缺少以下依赖项可能会导致相同的异常
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
我不知道为什么,但我现在发现,当反序列化过程中出现问题时,就会发生这种错误。检查您的映射类别。
使用
.accept(MediaType.APPLICATION_JSON)
如
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();