RestUtil

package com.example.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.*;

@Slf4j
public class RestClientUtil {

    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;

    public RestClientUtil(RestTemplate restTemplate, ObjectMapper objectMapper) {
        this.restTemplate = restTemplate;
        this.objectMapper = objectMapper;
    }

    public RequestBuilder request() {
        return new RequestBuilder();
    }

    public class RequestBuilder {

        private HttpMethod method = HttpMethod.GET;
        private String url;
        private Map<String, ?> urlParams = Collections.emptyMap();
        private final Map<String, String> headers = new LinkedHashMap<>();
        private Object body;
        private MediaType contentType;
        private TypeReference<?> responseType;

        // 新增:form参数收集
        private final MultiValueMap<String, String> formParams = new LinkedMultiValueMap<>();

        private RequestBuilder() {
            headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
            headers.put(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.name());
        }

        public RequestBuilder method(HttpMethod method) {
            this.method = method;
            return this;
        }

        public RequestBuilder url(String url) {
            this.url = url;
            return this;
        }

        public RequestBuilder urlParams(Map<String, ?> urlParams) {
            if (urlParams != null) {
                this.urlParams = urlParams;
            }
            return this;
        }

        public RequestBuilder addHeader(String name, String value) {
            if (name != null && value != null) {
                headers.put(name, value);
            }
            return this;
        }

        public RequestBuilder headers(Map<String, String> headers) {
            if (headers != null) {
                this.headers.putAll(headers);
            }
            return this;
        }

        public RequestBuilder body(Object body) {
            this.body = body;
            return this;
        }

        public RequestBuilder contentType(MediaType contentType) {
            this.contentType = contentType;
            return this;
        }

        public <T> RequestBuilder responseType(TypeReference<T> responseType) {
            this.responseType = responseType;
            return this;
        }

        /**
         * 新增:添加form单个参数,链式调用
         */
        public RequestBuilder formParam(String key, String value) {
            if (key != null && value != null) {
                formParams.add(key, value);
            }
            return this;
        }

        public <T> T execute() {
            if (url == null || url.isEmpty()) {
                throw new IllegalArgumentException("请求URL不能为空");
            }
            if (responseType == null) {
                throw new IllegalArgumentException("必须设置响应类型 responseType");
            }

            // 如果有formParams,优先使用form请求体,Content-Type自动设置
            if (!formParams.isEmpty()) {
                this.contentType = MediaType.APPLICATION_FORM_URLENCODED;
                this.body = formParams;
            }

            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setAll(headers);
            if (contentType != null) {
                httpHeaders.setContentType(contentType);
            }
            httpHeaders.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));

            HttpEntity<?> entity;

            try {
                if (body == null) {
                    entity = new HttpEntity<>(httpHeaders);
                } else if (MediaType.APPLICATION_JSON.includes(contentType)) {
                    String jsonBody = body instanceof String ? (String) body : objectMapper.writeValueAsString(body);
                    entity = new HttpEntity<>(jsonBody, httpHeaders);
                } else if (MediaType.APPLICATION_FORM_URLENCODED.includes(contentType)) {
                    if (!(body instanceof MultiValueMap)) {
                        throw new IllegalArgumentException("Form请求体必须是 MultiValueMap 类型");
                    }
                    entity = new HttpEntity<>(body, httpHeaders);
                } else if (MediaType.TEXT_PLAIN.includes(contentType)) {
                    entity = new HttpEntity<>(Objects.toString(body), httpHeaders);
                } else {
                    entity = new HttpEntity<>(body, httpHeaders);
                }
            } catch (JsonProcessingException e) {
                log.error("请求体序列化异常", e);
                throw new RestClientException("请求体序列化异常", e);
            }

            log.info("请求信息: method={}, url={}, urlParams={}, headers={}, body={}, contentType={}",
                    method, url, urlParams, headers, body, contentType);

            try {
                ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, entity, String.class, urlParams);
                log.info("响应状态码: {}", responseEntity.getStatusCodeValue());
                if (responseEntity.hasBody()) {
                    log.info("响应体: {}", responseEntity.getBody());
                } else {
                    log.warn("响应无内容");
                }

                if (!responseEntity.getStatusCode().is2xxSuccessful()) {
                    throw new RestClientException("HTTP请求失败,状态码:" + responseEntity.getStatusCodeValue());
                }

                String respBody = responseEntity.getBody();
                if (respBody == null || respBody.isEmpty()) {
                    return null;
                }

                @SuppressWarnings("unchecked")
                T result = (T) objectMapper.readValue(respBody, responseType);
                return result;
            } catch (Exception e) {
                log.error("请求执行异常", e);
                throw new RestClientException("请求执行异常", e);
            }
        }
    }
}
 

参考这个工具package org.jeecg.common.es; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.jeecg.common.util.RestUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.Set; /** * 关于 ElasticSearch 的一些方法(创建索引、添加数据、查询等) * * @author sunjianlei */ @Slf4j @Component public class JeecgElasticsearchTemplate { /** * 用户配置是否通过,未通过就不走任何方法 */ private static boolean configIsPassed = true; /** * 是否已检测过配置 */ private static boolean configIsChecked = false; private String baseUrl; private final String FORMAT_JSON = "format=json"; public JeecgElasticsearchTemplate(@Value("${jeecg.elasticsearch.cluster-nodes}") String baseUrl) { log.debug("JeecgElasticsearchTemplate BaseURL:" + baseUrl); // 未检测过配置,进行检测操作 if (!configIsChecked) { configIsChecked = true; // 为空则代表未配置 baseUrl if (StringUtils.isEmpty(baseUrl)) { configIsPassed = false; } else { this.baseUrl = baseUrl; // 判断配置的地址是否有效 try { RestUtil.get(this.getBaseUrl().toString()); } catch (Exception e) { configIsPassed = false; } } if (configIsPassed) { log.info("ElasticSearch服务连接成功"); } else { log.warn("ElasticSearch 服务连接失败,原因:配置未通过。可能是BaseURL未配置或配置有误,也可能是Elasticsearch服务未启动。接下来将会拒绝执行任何方法!"); } } } /** * 检查配置是否通过,未通过就抛出异常,中断执行 */ private void checkConfig() { if (!configIsPassed) { throw new RuntimeException("配置未通过,拒绝执行该方法"); } } public StringBuilder getBaseUrl(String indexName, String typeName) { typeName = typeName.trim().toLowerCase(); return this.getBaseUrl(indexName).append("/").append(typeName); } public StringBuilder getBaseUrl(String indexName) { indexName = indexName.trim().toLowerCase(); return this.getBaseUrl().append("/").append(indexName); } public StringBuilder getBaseUrl() { return new StringBuilder("http://").append(this.baseUrl); } /** * cat 查询ElasticSearch系统数据,返回json */ public <T> ResponseEntity<T> _cat(String urlAfter, Class<T> responseType) { this.checkConfig(); String url = this.getBaseUrl().append("/_cat").append(urlAfter).append("?").append(FORMAT_JSON).toString(); return RestUtil.request(url, HttpMethod.GET, null, null, null, responseType); } /** * 查询所有索引 * <p> * 查询地址:GET http://{baseUrl}/_cat/indices */ public JSONArray getIndices() { this.checkConfig(); return getIndices(null); } /** * 查询单个索引 * <p> * 查询地址:GET http://{baseUrl}/_cat/indices/{indexName} */ public JSONArray getIndices(String indexName) { this.checkConfig(); StringBuilder urlAfter = new StringBuilder("/indices"); if (!StringUtils.isEmpty(indexName)) { urlAfter.append("/").append(indexName.trim().toLowerCase()); } return _cat(urlAfter.toString(), JSONArray.class).getBody(); } /** * 索引是否存在 */ public boolean indexExists(String indexName) { this.checkConfig(); try { JSONArray array = getIndices(indexName); return array != null; } catch (org.springframework.web.client.HttpClientErrorException ex) { if (HttpStatus.NOT_FOUND == ex.getStatusCode()) { return false; } else { throw ex; } } } /** * 创建索引 * <p> * 查询地址:PUT http://{baseUrl}/{indexName} */ public boolean createIndex(String indexName) { this.checkConfig(); String url = this.getBaseUrl(indexName).toString(); /* 返回结果 (仅供参考) "createIndex": { "shards_acknowledged": true, "acknowledged": true, "index": "hello_world" } */ try { return RestUtil.put(url).getBoolean("acknowledged"); } catch (org.springframework.web.client.HttpClientErrorException ex) { if (HttpStatus.BAD_REQUEST == ex.getStatusCode()) { log.warn("索引创建失败:" + indexName + " 已存在,无需再创建"); } else { ex.printStackTrace(); } } return false; } /** * 删除索引 * <p> * 查询地址:DELETE http://{baseUrl}/{indexName} */ public boolean removeIndex(String indexName) { this.checkConfig(); String url = this.getBaseUrl(indexName).toString(); try { return RestUtil.delete(url).getBoolean("acknowledged"); } catch (org.springframework.web.client.HttpClientErrorException ex) { if (HttpStatus.NOT_FOUND == ex.getStatusCode()) { log.warn("索引删除失败:" + indexName + " 不存在,无需删除"); } else { ex.printStackTrace(); } } return false; } /** * 保存数据,详见:saveOrUpdate */ public boolean save(String indexName, String typeName, String dataId, JSONObject data) { this.checkConfig(); return this.saveOrUpdate(indexName, typeName, dataId, data); } /** * 更新数据,详见:saveOrUpdate */ public boolean update(String indexName, String typeName, String dataId, JSONObject data) { this.checkConfig(); return this.saveOrUpdate(indexName, typeName, dataId, data); } /** * 保存或修改索引数据 * <p> * 查询地址:PUT http://{baseUrl}/{indexName}/{typeName}/{dataId} * * @param indexName 索引名称 * @param typeName type,一个任意字符串,用于分 * @param dataId 数据id * @param data 要存储的数据 * @return */ public boolean saveOrUpdate(String indexName, String typeName, String dataId, JSONObject data) { this.checkConfig(); String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString(); /* 返回结果(仅供参考) "createIndexA2": { "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_index": "test_index_1", "_type": "test_type_1", "_id": "a2", "_version": 1, "_primary_term": 1 } */ try { // 去掉 data 中为空的值 Set<String> keys = data.keySet(); List<String> emptyKeys = new ArrayList<>(keys.size()); for (String key : keys) { String value = data.getString(key); if (StringUtils.isEmpty(value)) { emptyKeys.add(key); } } for (String key : emptyKeys) { data.remove(key); } } catch (Exception e) { e.printStackTrace(); } String result = RestUtil.put(url, data).getString("result"); return "created".equals(result) || "updated".equals(result); } /** * 删除索引数据 * <p> * 请求地址:DELETE http://{baseUrl}/{indexName}/{typeName}/{dataId} */ public boolean delete(String indexName, String typeName, String dataId) { this.checkConfig(); String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString(); /* 返回结果(仅供参考) { "_index": "es_demo", "_type": "docs", "_id": "001", "_version": 3, "result": "deleted", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 28, "_primary_term": 18 } */ try { return "deleted".equals(RestUtil.delete(url).getString("result")); } catch (org.springframework.web.client.HttpClientErrorException ex) { if (HttpStatus.NOT_FOUND == ex.getStatusCode()) { return false; } else { throw ex; } } } /* = = = 以下关于查询和查询条件的方法 = = =*/ /** * 查询数据 * <p> * 请求地址:POST http://{baseUrl}/{indexName}/{typeName}/_search */ public JSONObject search(String indexName, String typeName, JSONObject queryObject) { this.checkConfig(); String url = this.getBaseUrl(indexName, typeName).append("/_search").toString(); log.info("search: " + queryObject.toJSONString()); return RestUtil.post(url, queryObject); } /** * 查询数据 * <p> * 请求地址:POST http://{baseUrl}/{indexName}/{typeName}/_search */ public JSONObject mapping(String indexName, String typeName, JSONObject queryObject) { this.checkConfig(); String url = this.getBaseUrl(indexName, typeName).append("/_mapping").toString(); log.info("search: " + queryObject.toJSONString()); return RestUtil.post(url, queryObject); } /** * @return { "query": query } */ public JSONObject buildQuery(JSONObject query) { JSONObject json = new JSONObject(); json.put("query", query); return json; } /** * @return { "bool" : { "must": must, "must_not": mustNot, "should": should } } */ public JSONObject buildBoolQuery(JSONArray must, JSONArray mustNot, JSONArray should) { JSONObject bool = new JSONObject(); if (must != null) { bool.put("must", must); } if (mustNot != null) { bool.put("must_not", mustNot); } if (should != null) { bool.put("should", should); } JSONObject json = new JSONObject(); json.put("bool", bool); return json; } /** * @param field 要查询的字段 * @param args 查询参数,参考: *哈哈* OR *哒* NOT *呵* OR *啊* * @return */ public JSONObject buildQueryString(String field, String... args) { if (field == null) { return null; } StringBuilder sb = new StringBuilder(field).append(":("); if (args != null) { for (String arg : args) { sb.append(arg).append(" "); } } sb.append(")"); return this.buildQueryString(sb.toString()); } /** * @return { "query_string": { "query": query } } */ public JSONObject buildQueryString(String query) { JSONObject queryString = new JSONObject(); queryString.put("query", query); JSONObject json = new JSONObject(); json.put("query_string", queryString); return json; } /** * @param field 查询字段 * @param min 最小值 * @param max 最大值 * @param containMin 范围内是否包含最小值 * @param containMax 范围内是否包含最大值 * @return { "range" : { field : { 『 "gt『e』?containMin" : min 』?min!=null , 『 "lt『e』?containMax" : max 』}} } */ public JSONObject buildRangeQuery(String field, Object min, Object max, boolean containMin, boolean containMax) { JSONObject inner = new JSONObject(); if (min != null) { if (containMin) { inner.put("gte", min); } else { inner.put("gt", min); } } if (max != null) { if (containMax) { inner.put("lte", max); } else { inner.put("lt", max); } } JSONObject range = new JSONObject(); range.put(field, inner); JSONObject json = new JSONObject(); json.put("range", range); return json; } } 写一个添加映射方法
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值