spring Java:伪客户端中的默认值

ryoqjall  于 2022-11-21  发布在  Spring
关注(0)|答案(2)|浏览(122)

告诉我,如何在Feign客户端或其他中设置参数的默认值?
下面是我的代码。我指定了默认值,但它不起作用:(
服务项目:

public Price get(PricesRequest request) {
        return  priceFeignClient.get(
                       request.getPrice(),
                       request.getAddress(),
                       request.getCode(),
                       request.getCurrency()
                )
}

伪装客户:

public interface PriceFeignClient {
    @GetMapping
    Price get(@RequestParam("price") String price,
              @RequestParam("address") String Address,
              @RequestParam(value = "code", required = true, defaultValue = "AAA") String code,
              @RequestParam("currency") String currency
    );

我想为**“code”**参数设置一个默认值。

aelbi1ox

aelbi1ox1#

该问题是这样解决的:
我把这个添加到我正在敲的服务器上的主API中(即,在接收端,而不是在发送端请求)。

@RequestParam(value = "code", required = false, defaultValue = "AAA") String code

必需的是必需=假(而不是 * 必需=真 *)。

qni6mghb

qni6mghb2#

如果您想在客户端执行此操作,您可以尝试在FeignClient接口中添加一个默认方法来传递固定值。例如:

public interface PriceFeignClient {
    @GetMapping
    Price get(@RequestParam("price") String price,
              @RequestParam("address") String Address,
              @RequestParam("code") String code,
              @RequestParam("currency") String currency
    );

    default Price get(String price, String address, String currency) {
       return this.get(price, address, "AAA", currency);
    }
}

在你的服务中你这样称呼它:

public Price get(PricesRequest request) {
   return priceFeignClient.get(
                 request.getPrice(),
                 request.getAddress(),
                 request.getCurrency()
          );
}

相关问题