无法反序列化 <Object> Out of START_ARRAY 令牌的实例 - webClient



我正在使用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 tokenn 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"
    }
]

以下是我的控制器类中获取属性的方法:

@RequestMapping(method = RequestMethod.GET, value = "/getentity")
    public Mono<Entity> getEntity(@RequestParam("token") String token, @RequestParam("name") String name) {
        return service.fetchEntity(token, name);
    }

我的提取方法是:

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;
    }
}

ModernMentity模型是:

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"
    }
  ]
}

如果您希望杰克逊直接对物体进行反序列化,则必须这样说:

Flux<ModernMsgEntity> messages = webClient.get().uri(url).retrieve().bodyToFlux(ModernMsgEntity.class);

相关内容

  • 没有找到相关文章

最新更新