使用
package com.thunisoft.sspt.bootstrap; import com.thunisoft.maybee.engine.restclient.RestClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Configuration public class LocalRestClient { @Autowired private RestTemplate restTemplate; @Bean public RestClient restClient() { Map<String, String> server = new HashMap<>(); server.put("scheme", "http"); server.put("host", "localhost"); server.put("port", "9999"); List<Map<String, String>> servers = new ArrayList<>(); servers.add(server); return new RestClient(restTemplate, servers); } }
类
package com.thunisoft.maybee.engine.restclient; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.thunisoft.maybee.engine.request.RequestUtil; import com.thunisoft.maybee.engine.utils.JsonUtil; import com.thunisoft.maybee.engine.utils.MapUrlParamsUtils; import com.thunisoft.maybee.engine.utils.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.*; /** * default RestTemplate class extends. */ public class RestClient { private List<Map<String, String>> serverAddress; @Autowired private RestTemplate restTemplate; public RestClient() { } public RestClient(RestTemplate restTemplate, List<Map<String, String>> serverAddress) { this.setRestTemplate(restTemplate); this.setServerAddress(serverAddress); } /** * 以GET方式操作服务,主要用于查询操作 * * @param serviceMethod * @param clazz * @param params * @param <T> 集合对象或者单一对象 统一使用 T.class * @return */ public <T> T getService(String serviceMethod, Class<T> clazz, Map<String, Object> params) { return excuteService("GET", serviceMethod, clazz, params, null); } /** * 以POST方式操作服务,主要用于增删改 * * @param serviceMethod * @param clazz * @param params * @param headers * @param <T> 增加、删除、更改 建议使用 String.class * @return */ public <T> T postService(String serviceMethod, Class<T> clazz, Map<String, Object> params, HttpHeaders headers) { return excuteService("POST", serviceMethod, clazz, params, headers); } /** * 服务执行方法 * * @param httpMethod * @param serviceMethod * @param clazz * @param params * @param headers * @param <T> * @return */ private <T> T excuteService(String httpMethod, String serviceMethod, Class<T> clazz, Map<String, Object> params, HttpHeaders headers) { Map<String, Object> otherParams = new HashMap<>(); // otherParams.put("accessToken", RequestUtil.getAccessToken()); // put in the HTTP headers. if (headers == null) { headers = new HttpHeaders(); } headers.add("accessToken", RequestUtil.getAccessToken()); Map<String, Object> requestParams = MapUtils.addParams(params, otherParams); String urlParamsByMap = MapUrlParamsUtils.getUrlParamsByMap(requestParams); Map<String, String> randomOneServer = null; try { randomOneServer = this.getRandomOneServer(); } catch (Exception e) { // nothing... System.out.println("server is null : " + e.getMessage()); } URI uri = null; try { if (HttpMethod.POST.matches(httpMethod)) { uri = new URI(randomOneServer.get("scheme"), null, randomOneServer.get("host"), Integer.valueOf(randomOneServer.get("port")), serviceMethod, null, null); } else if (HttpMethod.GET.matches(httpMethod)) { uri = new URI(randomOneServer.get("scheme"), null, randomOneServer.get("host"), Integer.valueOf(randomOneServer.get("port")), serviceMethod, urlParamsByMap, null); } } catch (URISyntaxException e) { // nothing... System.out.println(e.getMessage()); return null; } ObjectMapper mapper = new ObjectMapper(); if (HttpMethod.GET.matches(httpMethod)) { // String forObject = this.getRestTemplate().getForObject(uri, String.class); HttpEntity httpEntity = new HttpEntity(headers); ResponseEntity<String> exchange = this.getRestTemplate().exchange(uri, HttpMethod.GET, httpEntity, String.class); String forObject = exchange.getBody(); JsonUtil.JSON_TYPE jsonType = JsonUtil.getJSONType(forObject); try { switch (jsonType) { case JSON_TYPE_ARRAY: JavaType javaType = getCollectionType(mapper, ArrayList.class, clazz); return mapper.readValue(forObject, javaType); case JSON_TYPE_OBJECT: return mapper.readValue(forObject, clazz); default: return null; } } catch (IOException e) { // nothing... System.out.println(e.getMessage()); return null; } } else if (HttpMethod.POST.matches(httpMethod)) { MultiValueMap reqParams = new LinkedMultiValueMap(); Set<Map.Entry<String, Object>> entries = requestParams.entrySet(); for (Map.Entry<String, Object> entry : entries) { reqParams.add(entry.getKey(), entry.getValue()); } HttpEntity httpEntity = new HttpEntity(reqParams, headers); return this.getRestTemplate().postForObject(uri.toString(), httpEntity, clazz); } else { return null; } } /** * 获取泛型的Collection Type * * @param collectionClass 泛型的Collection * @param elementClasses 元素类 * @return JavaType Java类型 * @since 1.0 */ private JavaType getCollectionType(ObjectMapper mapper, Class<?> collectionClass, Class<?>... elementClasses) { return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); } /** * 随机获取一个服务 * * @return */ private Map<String, String> getRandomOneServer() throws Exception { if (this.getServerAddress() == null) { throw new Exception("配置为空!"); } Random rand = new Random(); return this.getServerAddress().get(rand.nextInt(this.getServerAddress().size())); } public RestTemplate getRestTemplate() { return restTemplate; } public void setRestTemplate(RestTemplate restTemplate) { this.restTemplate = restTemplate; } public List<Map<String, String>> getServerAddress() { return serverAddress; } public void setServerAddress(List<Map<String, String>> serverAddress) { this.serverAddress = serverAddress; } }