| New file | 
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|