简体   繁体   中英

Error Spring React REST Controller Using Custom Class Response (WebFlux)

I'm trying to build a Spring WebFlux project and realize the follow business logic:

1 - Call an external REST Api using WebClient and parse the Json results using the Models below. It is working OK

2 - To show the Mono results Mono<DeviceList> devices , I'm using the ResponseApi class and returning it, but it is NOT working

I'm getting the follow error: Response status 406 with reason "Could not find acceptable representation"

Thanks


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

You can get devices, then in non blocking way, map them to the ResponseApi like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM