响应式弹簧查询参数 [英] Reactive Spring Query Parameters

查看:111
本文介绍了响应式弹簧查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spring Reactive Web API应该能够基于查询参数进行过滤,但是我正在努力做到这一点.

My Spring Reactive Web API should be able to filter based on query parameters but I'm struggling to get this right.

GET /v1/address?type=business

应在系统中返回所有类型为"business"的地址(仅作为示例,因为我没有发布实际要求).如果不存在该参数,则只需返回所有地址即可.

Should return all addresses of type 'business' in the system (just an example since I'm not posting the actual requirements). If the parameter is absent then just return all addresses.

[
  { "id" : 1, "type" : "business", "street" : "..."},
  { "id" : 2, "type" : "business", "street" : "..."},
  ...
}

我正在使用路由器功能来映射请求:

I'm using Router functions to map the request:

public RouterFunction<ServerResponse> route(AddressHandler handler) {
    return route(GET("/v1/address"), handler::getAddresses);
}

但是我正在努力确定如何检查参数,如果不存在该参数,则返回所有地址.

But I'm struggling to determine how to check for the parameter and, if it isn't present, to return all addresses.

class AddressHandler {
    Mono<ServerResponse> getAddressesFilteredOrNot(ServerRequest request) {
        Optional<String> addressType = request.getQueryParam("type");

        ...
    }
}

现在Optional.ifPresent()无法正常工作,我无法从方法中返回:

Now the Optional.ifPresent() isn't going to work, I can't return from the method:

class AddressHandler {
    Mono<ServerResponse> getAddressesFilteredOrNot(ServerRequest request) {
        Optional<String> addressType = request.getQueryParam("type");

        // invalid
        addressType.ifPresent(addrType -> {
            return ServerResponse.ok()
                .body(this.service.getAddressByType(addrType),
                   Address.class);
    });
}

我也不能在参数上切换ifEmpEmpty(),因为它抛出NullPointerException:

Also I can't switchIfEmpty() on the parameter since it throws a NullPointerException:

class AddressHandler {
    Mono<ServerResponse> getAddressesFilteredOrNot(ServerRequest request) {
        Optional<String> addressType = request.getQueryParam("type");

        // also invalid
        return Mono.justOrEmpty(addressType.orElse(""))
               .flatMap(addrType -> ServerResponse.ok().body(this.service.getAddressByType(addrType), Address.class)
               .switchIfEmpty(ServerResponse.ok().body(this.service.getAllAddresses(), Address.class));
    });
}

使用查询参数(如果存在)又使用getAllAddresses()方法的流程是什么?真的有可能吗?

What's the flow to use query parameters if they are present and fall back on the getAllAddresses() method? Is it actually possible?

推荐答案

您可以使用OptionalorElse()orElseGet()功能来调用替代方法.例如:

You can use the orElse() or orElseGet() functionality of Optional to invoke an alternative method. For example:

request
    .getQueryParam("type")
    .map(type -> service.getAddressByType(type))
    .orElseGet(() -> service.getAllAddresses());

在这种情况下,orElseGet()更有意义,因为它是延迟调用的.请注意,尽管这两个方法应该具有相同的返回类型,所以可能是Flux<Address>.

In this case, orElseGet() makes more sense, because it's lazily invoked. Be aware though that both methods should have the same return type, so probably Flux<Address>.

这篇关于响应式弹簧查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆