使用自定义类响应(WebFlux)出错的Spring React REST控制器

时间:2018-10-30 11:51:34

标签: spring reactive

我正在尝试构建一个Spring WebFlux项目并实现以下业务逻辑:

1-使用WebClient调用外部REST Api,并使用以下模型解析Json结果。一切正常

2-为了显示Mono结果Mono<DeviceList> devices,我正在使用ResponseApi类并返回它,但是它不起作用

我收到以下错误: Response status 406 with reason "Could not find acceptable representation"

谢谢


# Json Result
{
    "data": [
        {
            "id": "5bc3c0efe833d93f401bafa8",
            "name": "XXXXX",
            "group": "5b8fd1fa0499f54cfa7febb8",
            "description": "Geolocalizacao gps",
            "payloadType": "None",
            "contract": "5bc08be5e833d93f40e1f936",
            "keepAlive": 0
        }
    ]
}

# Controller
public class DeviceController{
    ...
    ...

    @RequestMapping(value = V1 + BASE_URL + "/devices/types", method = GET, produces = APPLICATION_JSON)
    public Mono<ServerResponse> getDeviceTypes(){
        Mono<DeviceList> devices = deviceService.findDeviceTypes();

        ResponseApi r = new ResponseApi();
        r.setMessage("Test");
        r.setCode("200");
        r.setStatus(200);
        r.setData(devices);

        return ok().body(Mono.just(r), ResponseApi.class);
    }
}   

# Repository
public Mono<DeviceList> findDeviceTypes() {
    return webClient.get()
            .uri(DEVICE_TYPES_URL)
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .bodyToMono(DeviceList.class);
}

# Model
public class DeviceList{

    @JsonProperty("data")
    private List<Device> data;

    public List<Device> getData() {
        return data;
    }

    public void setData(List<Device> data) {
        this.data = data;
    }
}

public class Device{

    @JsonProperty("id")
    private String id;

    @JsonProperty("name")
    private String name;

    @JsonProperty("group")
    private String group;

    @JsonProperty("description")
    private String description;

    @JsonProperty("keepAlive")
    private Integer keepAlive;

    @JsonProperty("payloadType")
    private String payloadType;

    @JsonProperty("contract")
    private String contract;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(Integer keepAlive) {
        this.keepAlive = keepAlive;
    }

    public String getPayloadType() {
        return payloadType;
    }

    public void setPayloadType(String payloadType) {
        this.payloadType = payloadType;
    }

    public String getContract() {
        return contract;
    }

    public void setContract(String contract) {
        this.contract = contract;
    }
}

@JsonRootName("data")
public class ResponseApi{

    @JsonProperty("status")
    private Integer status;

    @JsonProperty("code")
    private String code;

    @JsonProperty("message")
    private String message;

    @JsonProperty("data")
    private Object data;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以获取设备,然后以非阻塞方式将它们映射到ResponseApi,如下所示:

@RequestMapping(value = V1 + BASE_URL + "/devices/types", method = GET, produces = APPLICATION_JSON)
public Mono<ServerResponse> getDeviceTypes(){
    return deviceService.findDeviceTypes()
            .flatMap(devices -> {
                ResponseApi r = new ResponseApi();
                r.setMessage("Test");
                r.setCode("200");
                r.setStatus(200);
                r.setData(devices);
                return ok().body(Mono.just(r), ResponseApi.class);
            });
}