2a22b461a9e3a0c1d603937c817b592ff3c8a156
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.admin.bff.configuration;
2
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;
10
11 import java.io.IOException;
12 import java.util.*;
13
14 /**
15  * feign请求参数转化
16  * @author fengpy
17  */
18 @Configuration
19 public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
20     @Autowired
21     private ObjectMapper objectMapper;
22
23     public FeignBasicAuthRequestInterceptor() {}
24
25     @Override
26     public void apply(RequestTemplate template) {
27         ///**get-pojo贯穿*/
28         if (template.method().equals("GET") && template.request().body() != null) {
29             try {
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) {
37                 e.printStackTrace();
38             }
39         }
40     }
41
42     //处理 get-pojo贯穿
43     private void buildQuery(JsonNode jsonNode, String path, Map<String, Collection<String>> queries) {
44         if (!jsonNode.isContainerNode()) { //叶子节点
45             if (jsonNode.isNull()) {
46                 return;
47             }
48             Collection<String> values = queries.get(path);
49             if (null == values) {
50                 values = new ArrayList<>();
51                 queries.put(path, values);
52             }
53             values.add(jsonNode.asText());
54             return;
55         }
56         if (jsonNode.isArray()) { //数组节点
57             Iterator<JsonNode> it = jsonNode.elements();
58             while (it.hasNext()) {
59                 buildQuery(it.next(), path, queries);
60             }
61         } else {
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);
68                     } else {
69                         buildQuery(entry.getValue(), path + "." + entry.getKey(), queries);
70                     }
71                 } else { //根节点
72                     buildQuery(entry.getValue(), entry.getKey(), queries);
73                 }
74             }
75         }
76     }
77 }