1 package com.supwisdom.institute.backend.admin.bff.configuration;
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import feign.RequestInterceptor;
6 import feign.RequestTemplate;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.context.annotation.Configuration;
9 import org.springframework.util.StringUtils;
11 import java.io.IOException;
19 public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
21 private ObjectMapper objectMapper;
23 public FeignBasicAuthRequestInterceptor() {}
26 public void apply(RequestTemplate template) {
28 if (template.method().equals("GET") && template.request().body() != null) {
30 JsonNode jsonNode = objectMapper.readTree(template.request().body());
31 //template.body(null);
32 Map<String, Collection<String>> queries = new HashMap<>();
33 //feign 不支持 GET 方法传 POJO, json body转query
34 buildQuery(jsonNode, "", queries);
35 template.queries(queries);
36 } catch (IOException e) {
43 private void buildQuery(JsonNode jsonNode, String path, Map<String, Collection<String>> queries) {
44 if (!jsonNode.isContainerNode()) { //叶子节点
45 if (jsonNode.isNull()) {
48 Collection<String> values = queries.get(path);
50 values = new ArrayList<>();
51 queries.put(path, values);
53 values.add(jsonNode.asText());
56 if (jsonNode.isArray()) { //数组节点
57 Iterator<JsonNode> it = jsonNode.elements();
58 while (it.hasNext()) {
59 buildQuery(it.next(), path, queries);
62 Iterator<Map.Entry<String, JsonNode>> it = jsonNode.fields();
63 while (it.hasNext()) {
64 Map.Entry<String, JsonNode> entry = it.next();
65 if (StringUtils.hasText(path)) {
66 if ("mapBean".equals(path)||"orderBy".equals(path)||"sequence".equals(path)) {
67 buildQuery(entry.getValue(), path + "[" + entry.getKey() + "]", queries);
69 buildQuery(entry.getValue(), path + "." + entry.getKey(), queries);
72 buildQuery(entry.getValue(), entry.getKey(), queries);