feign客户端get请求如何优雅地传递多个参数?

环境

  • spring-cloud-dependencies(Finchley.RELEASE)
  • spring-cloud-starter-openfeign

解决方法

方法一, 将一个复杂的参数转换成多个参数(不推荐)

import feign.QueryMap;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@FeignClient(value = "feign-demo")
public interface FeignService {

    @GetMapping("/getByParams")
    Object getByParams3(@RequestParam("name") String name,
                        @RequestParam("id") Long id,
                        @RequestParam("date") String date);

    @GetMapping("/getByParams")
    Object getByParams4(@RequestParam Map<String, Object> map);

    @GetMapping("/getByParams")
//    @RequestLine("GET /getByParams")
    Object getByParams5(@QueryMap Map<String, Object> map);
}

方法二, get请求+RequestBody注解(不推荐)

// feign 客户端
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "feign-demo")
public interface FeignService {

    @GetMapping("/getByParams")
    Object getByParams(@RequestBody ParamDTO paramDTO);
}

// 添加拦截器
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;


@Component
public class FeignRequestInterceptor implements RequestInterceptor {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void apply(RequestTemplate requestTemplate) {

        if (requestTemplate.method().equals("GET") && requestTemplate.body() != null) {
            try {
                JsonNode node = objectMapper.readTree(requestTemplate.body());
                requestTemplate.body(null);

                Map<String, Collection<String>> queries = new HashMap<>();
                buildQuery(node, "", queries);
                requestTemplate.queries(queries);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private void buildQuery(JsonNode jsonNode, String path, Map<String, Collection<String>> queries) {
        if (!jsonNode.isContainerNode()) {   // 叶子节点
            if (jsonNode.isNull()) {
                return;
            }
            Collection<String> values = queries.get(path);
            if (null == values) {
                values = new ArrayList<>();
                queries.put(path, values);
            }
            values.add(jsonNode.asText());
            return;
        }
        if (jsonNode.isArray()) {   // 数组节点
            Iterator<JsonNode> it = jsonNode.elements();
            while (it.hasNext()) {
                buildQuery(it.next(), path, queries);
            }
        } else {
            Iterator<Map.Entry<String, JsonNode>> it = jsonNode.fields();
            while (it.hasNext()) {
                Map.Entry<String, JsonNode> entry = it.next();
                if (StringUtils.hasText(path)) {
                    buildQuery(entry.getValue(), path + "." + entry.getKey(), queries);
                } else {  // 根节点
                    buildQuery(entry.getValue(), entry.getKey(), queries);
                }
            }
        }
    }
}

方法三, 使用okhttp替换feign默认的client(jdk底层的HttpURLConnection)(推荐)

// 引入依赖
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
        </dependency>

// 添加配置
feign:
  httpclient:
    enabled: false
  okhttp:
    enabled: true

// feign接口
import feign.QueryMap;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@FeignClient(value = "feign-demo")
public interface FeignService {

    @GetMapping("/getByParams")
    Object getByParams6(ParamDTO paramDTO);

}

小技巧

控制台如何打印feign的日志?

// 配置日志级别
logging:
  level:
    cn.springcloud.book.FeignService: debug

// 修改feign的日志级别, 全局
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值