我正在使用 webclient 编写一个简单的 get
方法来获取属性信息。但是,我收到以下错误响应消息:
{
"timestamp": "2019-02-25T06:57:03.487+0000",
"path": "/modernmsg/getentity",
"status": 500,
"error": "Internal Server Error",
"message": "JSON decoding error: Cannot deserialize instance of `com.reputation.api.modernmsg.model.Entity` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.reputation.api.modernmsg.model.Entity` out of START_ARRAY token\n at [Source: UNKNOWN; line: -1, column: -1]"
}
实际的 json 响应是:
[
{
"name": "Point Breeze",
"street": "488 Lemont Dr",
"city": "Nashville",
"state": "TN",
"postal_code": "37216",
"slug": "point-breeze"
}
]
下面是我的 Controller 类中获取属性的方法:
@RequestMapping(method = RequestMethod.GET, value = "/getentity")
public Mono<Entity> getEntity(@RequestParam("token") String token, @RequestParam("name") String name) {
return service.fetchEntity(token, name);
}
我的 fetchEntity 方法是:
public Mono<Entity> fetchEntity(String token, String name) {
String url = host + version + entityEndpoint + "?token=" + token + "&name=" + name;
return webClient.get().uri(url).retrieve().bodyToMono(Entity.class);
}
下面是我的实体模型:
package com.reputation.api.modernmsg.model;
import java.util.List;
public class Entity {
private List<ModernMsgEntity> modernMsgEntity;
public List<ModernMsgEntity> getModernMsgEntity() {
return modernMsgEntity;
}
public void setModernMsgEntity(List<ModernMsgEntity> modernMsgEntity) {
this.modernMsgEntity = modernMsgEntity;
}
}
ModernMsgEntity 模型是:
package com.reputation.api.modernmsg.model;
public class ModernMsgEntity {
private String name;
private String street;
private String city;
private String state;
private String postal_code;
private String slug;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostal_code() {
return postal_code;
}
public void setPostal_code(String postal_code) {
this.postal_code = postal_code;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
}
如果您需要更多信息,请告诉我。
最佳答案
这更多的是一个 JSON 反序列化问题。查看您的实体类,您正在将其设置为期望 JSON 响应,例如:
{
"modernMsgEntity": [
{
"name": "Point Breeze",
"street": "488 Lemont Dr",
"city": "Nashville",
"state": "TN",
"postal_code": "37216",
"slug": "point-breeze"
}
]
}
如果你希望 Jackson 直接反序列化一个对象数组,你必须这样告诉它:
Flux<ModernMsgEntity> messages = webClient.get().uri(url).retrieve().bodyToFlux(ModernMsgEntity.class);
关于reactive-programming - 无法从 START_ARRAY token 中反序列化 <Object> 的实例 - webClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54861283/