liurunyu
2025-03-26 595a52b5812c243918d91cfd339fb06491970abc
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.dy.rtuMw3rd.http4Xjnk;
 
import com.dy.common.util.HttpCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
 
/**
 * @Author: liurunyu
 * @Date: 2025/3/15 9:56
 * @Description
 */
@Component()
public class HttpRq {
 
    private RestTemplate restTemplate ;
 
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }
 
    /**
     * get请求,返回json内容
     * 同步
     * @param httpBaseUrl
     * @param params
     * @param cb
     * @throws Exception
     */
    public void get4JsonBySync(String httpBaseUrl, Map<String, String> params, HttpCallback cb) throws Exception {
        String url = UriComponentsBuilder.fromUriString(httpBaseUrl)
                .build()
                .toUriString();
 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) ;
        if(params != null && params.size() > 0){
            Iterator<String> it = params.keySet().iterator() ;
            String key ;
            while (it.hasNext()){
                key = it.next() ;
                builder.queryParam(key, params.get(key)) ;
            }
        }
        httpGetRequestSync(builder, cb);
    }
    /**
     * get请求,返回json内容
     * 异步
     * @param httpBaseUrl
     * @param params
     * @param cb
     * @throws Exception
     */
    public void get4JsonByAsync(String httpBaseUrl, Map<String, String> params, HttpCallback cb) throws Exception {
        String url = UriComponentsBuilder.fromUriString(httpBaseUrl)
                .build()
                .toUriString();
 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) ;
        if(params != null && params.size() > 0){
            Iterator<String> it = params.keySet().iterator() ;
            String key ;
            while (it.hasNext()){
                key = it.next() ;
                builder.queryParam(key, params.get(key)) ;
            }
        }
        httpGetRequestAsync(builder, cb);
    }
 
    /**
     * get请求,返回json内容
     * 同步
     * @param httpBaseUrl
     * @param params
     * @param cb
     * @throws Exception
     */
    public void post4JsonBySync(String httpBaseUrl, Map<String, String> params, HttpCallback cb) throws Exception {
        String url = UriComponentsBuilder.fromUriString(httpBaseUrl)
                .build()
                .toUriString();
 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) ;
        httpPostRequestSync(builder, params, cb);
    }
    /**
     * get请求,返回json内容
     * 异步
     * @param httpBaseUrl
     * @param params
     * @param cb
     * @throws Exception
     */
    public void post4JsonByAsync(String httpBaseUrl, Map<String, String> params, HttpCallback cb) throws Exception {
        String url = UriComponentsBuilder.fromUriString(httpBaseUrl)
                .build()
                .toUriString();
 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) ;
        httpPostRequestAsync(builder, params, cb);
    }
 
    /**
     * 发起请求
     * @param builder
     * @param cb
     * @return
     */
    private void httpGetRequestSync(UriComponentsBuilder builder, HttpCallback cb) {
        HttpHeaders headers = new HttpHeaders() ;
        headers.setContentType(MediaType.APPLICATION_JSON) ;
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        // 通过Get方式调用接口
        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),
                HttpMethod.GET,
                new HttpEntity<>(headers),
                String.class);
 
        String contentType = response.getHeaders().getContentType().toString() ;
        cb.call(response.getStatusCode().value() + "", contentType, response.getBody());
    }
 
 
    /**
     * 发起请求
     * @param builder
     * @param cb
     * @return
     */
    private void httpGetRequestAsync(UriComponentsBuilder builder, HttpCallback cb) throws Exception {
        HttpHeaders headers = new HttpHeaders() ;
        headers.setContentType(MediaType.APPLICATION_JSON) ;
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        // 发起异步 GET 请求
        CompletableFuture<ResponseEntity<String>> future = CompletableFuture.supplyAsync(() ->
                // 通过Get方式调用接口
                restTemplate.exchange(builder.toUriString(),
                        HttpMethod.GET,
                        new HttpEntity<>(headers),
                        String.class)
        );
        future.thenAccept(response -> {
            String contentType = response.getHeaders().getContentType().toString() ;
            cb.call(response.getStatusCode().value() + "", contentType, response.getBody());
        }) ;
        /*
        AtomicReference<Throwable> exeEx = new AtomicReference() ;
        future.thenAccept(response -> {
            String contentType = response.getHeaders().getContentType().toString() ;
            cb.call(response.getStatusCode().value() + "", contentType, response.getBody());
        }).exceptionally(ex -> {
            exeEx.set(ex.getCause());
            return null;
        });
        */
    }
 
    /**
     * 发起请求
     * @param builder
     * @param body
     * @param cb
     * @return
     */
    private void httpPostRequestSync(UriComponentsBuilder builder, Map<String, String> body, HttpCallback cb) {
        HttpHeaders headers = new HttpHeaders() ;
        headers.setContentType(MediaType.APPLICATION_JSON) ;
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(body, headers);
        // 通过Get方式调用接口
        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),
                HttpMethod.POST,
                requestEntity,
                String.class);
 
        String contentType = response.getHeaders().getContentType().toString() ;
        cb.call(response.getStatusCode().value() + "", contentType, response.getBody());
    }
 
    /**
     * 发起请求
     * @param builder
     * @param body
     * @param cb
     * @return
     */
    private void httpPostRequestAsync(UriComponentsBuilder builder, Map<String, String> body, HttpCallback cb) throws Exception {
        HttpHeaders headers = new HttpHeaders() ;
        headers.setContentType(MediaType.APPLICATION_JSON) ;
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(body, headers);
        // 发起异步 GET 请求
        CompletableFuture<ResponseEntity<String>> future = CompletableFuture.supplyAsync(() ->
                // 通过Get方式调用接口
                restTemplate.exchange(builder.toUriString(),
                        HttpMethod.POST,
                        requestEntity,
                        String.class)
        );
        future.thenAccept(response -> {
            String contentType = response.getHeaders().getContentType().toString() ;
            cb.call(response.getStatusCode().value() + "", contentType, response.getBody());
        }) ;
        /*
        AtomicReference<Throwable> exeEx = new AtomicReference() ;
        future.thenAccept(response -> {
            String contentType = response.getHeaders().getContentType().toString() ;
            cb.call(response.getStatusCode().value() + "", contentType, response.getBody());
        }).exceptionally(ex -> {
            exeEx.set(ex.getCause());
            return null;
        });
        */
    }
}