无法从START_ARRAY令牌中反序列化<object>的实例-WebClient

时间:2019-02-25 07:22:21

标签: reactive-programming webclient spring-webflux project-reactor

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

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

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

让我知道是否需要更多信息。

1 个答案:

答案 0 :(得分:0)

这更多是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);