1

org.springframework.web.reactive.function.client.WebClient在 Spring Boot (2.0.0.M1) 应用程序中使用来查询返回嵌套数组的 REST 接口:

[
    [ "name1", 2331.0, 2323.3 ],
    [ "name2", 2833.3, 3838.2 ]
]

我现在正试图将此响应映射到一个Flux对象。为此,我进行了以下调用:

WebClient webClient = WebClient.create("http://example.org");

Flux<Result> results = webClient.get().uri("/query").
    accept(MediaType.APPLICATION_JSON_UTF8).
    exchange().
    flatMapMany(response -> response.bodyToFlux(Result.class));

Result 类看起来像这样:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.math.BigDecimal;

@Data
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
public class Result {

    private final String name;
    private final BigDecimal value1;
    private final BigDecimal value2;

    @JsonCreator
    public Result(
        @JsonProperty String name,
        @JsonProperty BigDecimal value1,
        @JsonProperty BigDecimal value2) {
        this.name = name;
        this.value1 = value1;
        this.value2 = value2;
    }
}

不幸的是,我收到以下错误:

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=utf-8' not supported

谁能告诉我我做错了什么或告诉我一种更好的方法将这种响应反序列化为 Flux,最好以非阻塞方式?

4

1 回答 1

1

问题与Flux.

Jackson 根本无法反序列化您的 json 对象,并且可能无法public Result(@JsonProperty String name, @JsonProperty BigDecimal value1, @JsonProperty BigDecimal value2)使用一组不同的值来反序列化。

最简单的解决方法是使用下一个构造函数实现。

@JsonCreator
public Result(Object[] args) {
     this.name = String.valueOf(args[0]);
     this.value1 = new BigDecimal(String.valueOf(args[1]));
     this.value2 = new BigDecimal(String.valueOf(args[2]));
}
于 2017-05-29T14:43:37.593 回答