RopHttpUtil.java
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cn.com.glsx.entmicro.utils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilder;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author zhouhaibao
* @version 1.0
* @description: http调用rop端服务
* @date 2021/7/30 11:26
*/
public class RopHttpUtil {
private final static String appKey = "48e5e13229b82c1b4e6e8c96151f0637";
private final static String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";
private static void addBaseParam(MultiValueMap<String, String> paramsMap, String method) {
paramsMap.add("v", "1.0.0");
paramsMap.add("method", method);
paramsMap.add("name", "zhouhb");
}
public static <T> T get(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
addBaseParam(paramsMap, method);
MultiValueMap<String, String> params = getSysParams();
params.putAll(paramsMap);
for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
params.remove(entry.getKey());
}
}
String sign = RopUtils.sign(params.toSingleValueMap(), secret);
params.add("sign", sign);
RestTemplate rest = new RestTemplate();
RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(baseurl);
uriTemplateHandler.setDefaultUriVariables(params.toSingleValueMap());
rest.setUriTemplateHandler(uriTemplateHandler);
T forObject = rest.getForObject(baseurl, responseType, params.toSingleValueMap());
return forObject;
}
public static <T> T post(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
addBaseParam(paramsMap, method);
MultiValueMap<String, String> params = getSysParams();
params.putAll(paramsMap);
for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
params.remove(entry.getKey());
}
}
String sign = RopUtils.sign(params.toSingleValueMap(), secret);
params.add("sign", sign);
return new RestTemplate().postForObject(baseurl, params, responseType);
}
public static MultiValueMap<String, String> getSysParams() {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
params.add("appKey", appKey);
params.add("format", "json");
return params;
}
static class RopUriTemplateHandler extends DefaultUriBuilderFactory {
public RopUriTemplateHandler(String baseUriTemplate) {
super(baseUriTemplate);
}
@Override
public URI expand(String uriTemplate, Map<String, ?> uriVars) {
UriBuilder builder = uriString(uriTemplate);
if (uriVars != null && !uriVars.isEmpty()) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
for (Entry<String, ?> entry : uriVars.entrySet()) {
params.add(entry.getKey(), entry.getValue().toString());
}
builder.queryParams(params);
}
return builder.build(uriVars);
}
}
}