从Spring 5 WebFlux中的RouterFunction处理程序返回一个对象

时间:2018-01-03 12:00:06

标签: spring spring-boot spring-webflow project-reactor

我有一个RouterFunction的以下bean定义:

@Bean
    RouterFunction<?> routes(DestinationHandler destinationHandler) {

        return nest(path("/destination"), route(method(GET), destinationHandler::findAll));

    }

这就是处理函数:

public Mono<ServerResponse> findAll(ServerRequest request) {
    Flux<Destination> destinations = destinationService.findAll();

    ResponseData responseData = buildResponseData(destinations);

    return ok().body(Mono.just(responseData), ResponseData.class);

}

private ResponseData buildResponseData(Flux<Destination> destinations) {
    ResponseData responseData = new ResponseData();
    responseData.setStatus(HttpStatus.OK.value());
    responseData.setCode(DESTINATION_LIST_SUCCESS_CODE);
    responseData.setMessage(DESTINATION_LIST_SUCCESS_MESSAGE);
    responseData.setData(destinations);
    return responseData;
}

我的ResponseData:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData {
    private int status;
    private String code;
    private String message;
    private Object data;
}

目的地类:

//lombok annotations
public class Destination {
    private int id;
    private String local;
}

结果如下:

{
   “状态”:200,    “代码”:“destination_list_success”    “message”:“Retorno realizado com sucesso”,    “数据”:{
      “scanAvailable”:真实,       “预取”: - 1    } }

如何返回正确的目的地列表?

3 个答案:

答案 0 :(得分:2)

为此,您需要将Flux<Destination>转换为Mono<List<Destination>>,因此请进行以下更改:

buildResponseData(Flux<Destination> destinations)

为:

buildResponseData(List<Destination> destinations)

并将返回块更改为:

return ok().body(destinations.collectList().map(d-> buildResponseData(d)), ResponseData.class);

答案 1 :(得分:0)

试试这个

private ResponseData buildResponseData(Flux<Destination> destinations) 
{
    ResponseData responseData = new ResponseData();
    responseData.setStatus(HttpStatus.OK.value());
    responseData.setCode(DESTINATION_LIST_SUCCESS_CODE);
    responseData.setMessage(DESTINATION_LIST_SUCCESS_MESSAGE);
    responseData.setData(destinations.block());
    return responseData;
}

并将ResponseData类更改为

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData {
    private int status;
    private String code;
    private String message;
    private List<...> data;
}

答案 2 :(得分:0)

您应该取消订单。遵循卡洛斯的答案:

{
    "originalDetectIntentRequest": {
        "source": "google",
        "version": "2",
        "payload": {
          "surface": {
            "capabilities": [
              {
                "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
              },
              {
                "name": "actions.capability.AUDIO_OUTPUT"
              }
            ]
          },
          "inputs": [
            {
              "rawInputs": [
                {
                  "query": "one",
                  "inputType": "VOICE"
                }
              ],
              "arguments": [
                {
                  "rawText": "one",
                  "textValue": "one",
                  "name": "text"
                }
              ],
              "intent": "actions.intent.TEXT"
            }
          ],
          "user": {
            "lastSeen": "2018-10-26T12:19:26Z",
            "locale": "en-SG",
            "userId": "15405557729312087450498"
          },
          "conversation": {
            "conversationId": "ABwppHGPccKMD31_LlOcGGo9CWCtitXeXiZBedpNhqU0L-uc1uCdT8gsFGPeb3WKa6jCguq1gIaoHCcbdp2lqr8",
            "type": "ACTIVE",
            "conversationToken": "[\"actions_capability_audio_output\",\"actions_capability_media_response_audio\",\"fdd1b5e2-b11a-43d2-ac6d-eb92b3713792_id_dialog_context\",\"google_assistant_input_type_voice\",\"room_service_-_yes_dialog_context\",\"room_service_-_yes_dialog_params_order_quantity2\",\"roomservice-followup\"]"
          },
          "availableSurfaces": [
            {
              "capabilities": [
                {
                  "name": "actions.capability.SCREEN_OUTPUT"
                },
                {
                  "name": "actions.capability.WEB_BROWSER"
                },
                {
                  "name": "actions.capability.AUDIO_OUTPUT"
                }
              ]
            }
          ]
        }
      }

}