企微获客链接 中文乱码问题处理
- 问题背景
- 问题处理
- 补充内容
问题背景
为了推广产品,同时更好的服务客户,公司在接入企业微信后,需要用到企微获客链接相关推广操作,那么通过API 接口创建企微获客链接时,出现了中文乱码问题,比如
{"errcode":0,"errmsg":"ok","link":{"link_id":"ca8e631dfgsdg3858","link_name":"??????","url":"https://work.weixin.qq.com/ca/ca8e631dfgsdg3858","create_time":1717569991}}
而实际在通过 API 接口创建企微获客链接传入的 获客链接名称 link_name : “测试获客链接”
那么对于这样的问题如何处理呢?
问题处理
对于这种中文乱码的问题,一般来说通常可以通过设置请求头内容类型的方式来解决,那么首先查阅一下企业微信创建企微获客链接的 API 接口文档:https://developer.work.weixin.qq.com/document/path/97297#%E5%88%9B%E5%BB%BA%E8%8E%B7%E5%AE%A2%E9%93%BE%E6%8E%A5
接口文档中关于链接名称 link_name 并没有特殊的说明,那么我们只能基于以往经验通过设置请求头来进行尝试,为创建企微获客链接 的代码增加请求头信息
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
再次创建企微获客链接 可以看到正确返回了 中文名称的 企微获客链接
{"errcode":0,"errmsg":"ok","link":{"link_id":"cawcde5dbdafdfdaf98baf","link_name":"测试获客链接","url":"https://work.weixin.qq.com/ca/cawcde5dbdafdfdaf98baf","create_time":1717571008}}
那么到这里关于企微创建获客链接时 link_name 中文乱码的情况就解决了 。
补充内容
这里为了方便大家测试企微获客链接的 API 接口,我补充一下测试类相关代码,这样大家可以节省开发时间,测试类代码 WeChatTest.java
package com.ruoyi.web.controller.common;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;import java.util.*;/*** @Description com.dongao.test* @Author P001* @Date 2022/11/1 14:38* @Version 1.0*/
public class WeChatTest {private String accessToken;//企业微信的 企业idprivate final String wechatAppId = "你的企业id";//企业微信自建应用的 Secretprivate final String wechatSecret = "你的企业自建应用的Secret";/*** 获取accesstocken-自建应用 权限较小* @return*/public void getAccessToken () {String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+ wechatAppId+"&corpsecret="+wechatSecret;ResponseEntity<String> get = RestUtils.get(url, String.class);if (Objects.nonNull(get)) {String body = get.getBody();WeChatGettokenResponse res = JSON.parseObject(body, WeChatGettokenResponse.class);accessToken = res.getAccessToken();}}//创建企微获客链接 https://developer.work.weixin.qq.com/document/path/97297@Testpublic void createLink() {//获取accesstokenif (StringUtils.isEmpty(accessToken)) {getAccessToken();}if (StringUtils.isNotEmpty(accessToken)) {//组装参数JSONObject json = new JSONObject();json.put("link_name","测试获客链接");JSONObject range = new JSONObject();List<String> list = new ArrayList<String>();list.add("AoLiNa");range.put("user_list",list);json.put("range",range);json.put("skip_verify",false);String param = JSON.toJSONString(json);HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/create_link?access_token="+accessToken;ResponseEntity<String> post = RestUtils.post(url,httpHeaders,param,String.class);if (Objects.nonNull(post)) {String body = post.getBody();System.out.println(body);}}}//编辑企微获客链接 https://developer.work.weixin.qq.com/document/path/97297@Testpublic void updateLink() {//获取accesstokenif (StringUtils.isEmpty(accessToken)) {getAccessToken();}if (StringUtils.isNotEmpty(accessToken)) {//组装参数JSONObject json = new JSONObject();json.put("link_id","cawcde8e631eae3858");json.put("link_name","test2024");String param = JSON.toJSONString(json);HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/update_link?access_token="+accessToken;ResponseEntity<String> post = RestUtils.post(url,httpHeaders,param,String.class);if (Objects.nonNull(post)) {String body = post.getBody();System.out.println(body);}}}//获取企微获客链接 https://developer.work.weixin.qq.com/document/path/97297@Testpublic void getLinkList() {//获取accesstokenif (StringUtils.isEmpty(accessToken)) {getAccessToken();}if (StringUtils.isNotEmpty(accessToken)) {//组装参数JSONObject json = new JSONObject();/*json.put("limit",100);json.put("cursor","");*/String param = JSON.toJSONString(json);String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/list_link?access_token="+accessToken;ResponseEntity<String> post = RestUtils.post(url,param,String.class);if (Objects.nonNull(post)) {String body = post.getBody();System.out.println(body);}}}//获取企微获客链接详情 https://developer.work.weixin.qq.com/document/path/97297@Testpublic void getLinkDetailList() {//获取accesstokenif (StringUtils.isEmpty(accessToken)) {getAccessToken();}if (StringUtils.isNotEmpty(accessToken)) {//组装参数JSONObject json = new JSONObject();json.put("link_id","cawcde5db087898baf");String param = JSON.toJSONString(json);String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/get?access_token="+accessToken;ResponseEntity<String> post = RestUtils.post(url,param,String.class);if (Objects.nonNull(post)) {String body = post.getBody();System.out.println(body);}}}//删除企微获客链接@Testpublic void deleteLink() {//获取accesstokenif (StringUtils.isEmpty(accessToken)) {getAccessToken();}if (StringUtils.isNotEmpty(accessToken)) {//组装参数JSONObject json = new JSONObject();json.put("link_id","cawcde5db087898baf");String param = JSON.toJSONString(json);String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/delete_link?access_token="+accessToken;ResponseEntity<String> post = RestUtils.post(url,param,String.class);if (Objects.nonNull(post)) {String body = post.getBody();System.out.println(body);}}}//查询企微获客链接 剩余使用量@Testpublic void customerAcquisitionQuota() {//获取accesstokenif (StringUtils.isEmpty(accessToken)) {getAccessToken();}if (StringUtils.isNotEmpty(accessToken)) {String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition_quota?access_token="+accessToken;ResponseEntity<String> post = RestUtils.post(url,String.class);if (Objects.nonNull(post)) {String body = post.getBody();System.out.println(body);}}}
}
这里需要用到一个接收企业微信 access_token 的实体类,WeChatGettokenResponse.java 用于接收获取到的 企业微信 access_token ,
package com.ruoyi.web.controller.common;import com.taobao.api.internal.mapping.ApiField;/*** @Description com.dongao.project.common.wechat* @Author P001* @Date 2022/10/27 17:20* @Version 1.0*/
public class WeChatGettokenResponse extends WeChatResponse {private static final long serialVersionUID = 9059923845826321694L;@ApiField("access_token")private String accessToken;@ApiField("expires_in")private Long expiresIn;public String getAccessToken() {return accessToken;}public void setAccessToken(String accessToken) {this.accessToken = accessToken;}public Long getExpiresIn() {return expiresIn;}public void setExpiresIn(Long expiresIn) {this.expiresIn = expiresIn;}
}
其中需要用到实体类 WeChatResponse.java ,该实体类主要是接收企业微信请求返回的 code 码 以及 msg 信息等
package com.ruoyi.web.controller.common;import com.taobao.api.internal.mapping.ApiField;import java.io.Serializable;/*** @Description com.dongao.project.common.wechat.response* @Author P001* @Date 2022/11/3 13:41* @Version 1.0*/
public class WeChatResponse implements Serializable {private static final long serialVersionUID = 2109769518803249524L;@ApiField("errcode")private Long errcode;@ApiField("errmsg")private String errmsg;public Long getErrcode() {return errcode;}public void setErrcode(Long errcode) {this.errcode = errcode;}public String getErrmsg() {return errmsg;}public void setErrmsg(String errmsg) {this.errmsg = errmsg;}
}
最后补充的是在获取企业微信获客链接相关信息时,需要用到的Rest请求工具类,具体的 RestUtils.java 工具类代码如下
package com.ruoyi.web.controller.common;import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;import java.util.Collections;
import java.util.Map;
import java.util.Objects;/*** @description: RestUtils 远程调用工具类* @date: 2019/7/19 20:45* @author: dongao*/
public class RestUtils {private static final RestTemplate TEMPLATE = new RestTemplate();private static final int CONNECT_TIMEOUT = 10 * 1000;private static final int READ_TIMEOUT = 10 * 1000;/*** 记录请求日志*/static {// BufferingClientHttpRequestFactory因为我们要在拦截器和初始调用代码中都使用响应主体,所以这是必需的。默认实现只允许读取一次响应主体。 默认该流不可重复读SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();// 2022-03-10 设置统一超时时间requestFactory.setConnectTimeout(CONNECT_TIMEOUT);requestFactory.setReadTimeout(READ_TIMEOUT);TEMPLATE.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory));}// ----------------------------------GET-------------------------------------------------------/*** GET请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, Class<T> responseType) {return TEMPLATE.getForEntity(url, responseType);}/*** GET请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) {return TEMPLATE.getForEntity(url, responseType, uriVariables);}/*** GET请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) {return TEMPLATE.getForEntity(url, responseType, uriVariables);}/*** 带请求头的GET请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return get(url, httpHeaders, responseType, uriVariables);}/*** 带请求头的GET请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) {HttpEntity<?> requestEntity = new HttpEntity<>(headers);return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);}/*** 带请求头的GET请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return get(url, httpHeaders, responseType, uriVariables);}/*** 带请求头的GET请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<?> requestEntity = new HttpEntity<>(headers);return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);}/*** 自定义泛型返回GET请求调用方式** @param url 请求URL* @param responseBodyType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, ParameterizedTypeReference<T> responseBodyType) {return exchange(url, HttpMethod.GET, HttpEntity.EMPTY, responseBodyType);}/*** 自定义泛型返回GET请求调用方式** @param url 请求URL* @param responseBodyType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, ParameterizedTypeReference<T> responseBodyType, Object... uriVariables) {HttpHeaders headers = new HttpHeaders();//设置请求格式headers.setContentType(MediaType.APPLICATION_JSON_UTF8);//设置响应格式HttpEntity entity = new HttpEntity(headers);return exchange(url, HttpMethod.GET, entity, responseBodyType, uriVariables);}/*** 自定义泛型返回GET请求调用方式** @param url 请求URL* @param responseBodyType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> get(String url, ParameterizedTypeReference<T> responseBodyType, Map<String, ?> uriVariables) {return exchange(url, HttpMethod.GET, HttpEntity.EMPTY, responseBodyType, uriVariables);}/*** get queryString** @param url* @param formData* @param responseType* @param <T>* @return*/public static <T> ResponseEntity<T> getQueryString(String url, Map<String, ?> formData, Class<T> responseType) {StringBuilder queryString = new StringBuilder();for (Map.Entry<String, ?> entry : formData.entrySet()) {queryString.append("&").append(entry.getKey()).append("=");Object value = entry.getValue();if ( Objects.nonNull(value) ) {queryString.append(value);}}if ( queryString.length() > 0 ) {url = url.concat("?").concat(queryString.substring(1));}return get(url, responseType);}// ----------------------------------POST-------------------------------------------------------/*** POST请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @return*/public static <T> ResponseEntity<T> post(String url, Class<T> responseType) {return TEMPLATE.postForEntity(url, HttpEntity.EMPTY, responseType);}/*** POST请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType) {return TEMPLATE.postForEntity(url, requestBody, responseType);}/*** POST请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {return TEMPLATE.postForEntity(url, requestBody, responseType, uriVariables);}/*** POST请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {return TEMPLATE.postForEntity(url, requestBody, responseType, uriVariables);}/*** 带请求头的POST请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return post(url, httpHeaders, requestBody, responseType, uriVariables);}/*** 带请求头的POST请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);return post(url, requestEntity, responseType, uriVariables);}/*** 带请求头的POST请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return post(url, httpHeaders, requestBody, responseType, uriVariables);}/*** 带请求头的POST请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);return post(url, requestEntity, responseType, uriVariables);}/*** 自定义请求头和请求体的POST请求调用方式* * @param url 请求URL* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {return TEMPLATE.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);}/*** 自定义请求头和请求体的POST请求调用方式* * @param url 请求URL* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {return TEMPLATE.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);}/*** 自定义泛型返回POST请求调用方式** @param url 请求URL* @param responseBodyType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, ParameterizedTypeReference<T> responseBodyType) {return exchange(url, HttpMethod.POST, null, responseBodyType);}/*** 自定义泛型返回POST请求调用方式** @param url 请求URL* @param responseBodyType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, ParameterizedTypeReference<T> responseBodyType, Object... uriVariables) {return exchange(url, HttpMethod.POST, null, responseBodyType, uriVariables);}/*** 自定义泛型返回POST请求调用方式** @param url 请求URL* @param responseBodyType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> post(String url, ParameterizedTypeReference<T> responseBodyType, Map<String, ?> uriVariables) {return exchange(url, HttpMethod.POST, null, responseBodyType, uriVariables);}/*** post form-data** @param url* @param formData* @param responseType* @param <T>* @return*/public static <T> ResponseEntity<T> postFormData(String url, Map<String, String> formData, Class<T> responseType) {MultiValueMap<String,String> multiFormMap = new LinkedMultiValueMap<>();for (Map.Entry<String, String> entry : formData.entrySet()) {multiFormMap.put(entry.getKey(), Collections.singletonList(entry.getValue()));}HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(multiFormMap, headers);return post(url, httpEntity, responseType);}// ----------------------------------PUT-------------------------------------------------------/*** PUT请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, Class<T> responseType, Object... uriVariables) {return put(url, HttpEntity.EMPTY, responseType, uriVariables);}/*** PUT请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);return put(url, requestEntity, responseType, uriVariables);}/*** PUT请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);return put(url, requestEntity, responseType, uriVariables);}/*** 带请求头的PUT请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return put(url, httpHeaders, requestBody, responseType, uriVariables);}/*** 带请求头的PUT请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);return put(url, requestEntity, responseType, uriVariables);}/*** 带请求头的PUT请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return put(url, httpHeaders, requestBody, responseType, uriVariables);}/*** 带请求头的PUT请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);return put(url, requestEntity, responseType, uriVariables);}/*** 自定义请求头和请求体的PUT请求调用方式* * @param url 请求URL* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {return TEMPLATE.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);}/*** 自定义请求头和请求体的PUT请求调用方式* * @param url 请求URL* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {return TEMPLATE.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);}// ----------------------------------DELETE-------------------------------------------------------/*** DELETE请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Class<T> responseType, Object... uriVariables) {return delete(url, HttpEntity.EMPTY, responseType, uriVariables);}/*** DELETE请求调用方式* * @param url 请求URL* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Class<T> responseType, Map<String, ?> uriVariables) {return delete(url, HttpEntity.EMPTY, responseType, uriVariables);}/*** DELETE请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);return delete(url, requestEntity, responseType, uriVariables);}/*** DELETE请求调用方式* * @param url 请求URL* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);return delete(url, requestEntity, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return delete(url, httpHeaders, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);return delete(url, requestEntity, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return delete(url, httpHeaders, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);return delete(url, requestEntity, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return delete(url, httpHeaders, requestBody, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);return delete(url, requestEntity, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setAll(headers);return delete(url, httpHeaders, requestBody, responseType, uriVariables);}/*** 带请求头的DELETE请求调用方式* * @param url 请求URL* @param headers 请求头参数* @param requestBody 请求参数体* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);return delete(url, requestEntity, responseType, uriVariables);}/*** 自定义请求头和请求体的DELETE请求调用方式* * @param url 请求URL* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {return TEMPLATE.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);}/*** 自定义请求头和请求体的DELETE请求调用方式* * @param url 请求URL* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {return TEMPLATE.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);}// ----------------------------------通用方法-------------------------------------------------------/*** 通用调用方式* * @param url 请求URL* @param method 请求方法类型* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,按顺序依次对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {return TEMPLATE.exchange(url, method, requestEntity, responseType, uriVariables);}/*** 通用调用方式* * @param url 请求URL* @param method 请求方法类型* @param requestEntity 请求头和请求体封装对象* @param responseType 返回对象类型* @param uriVariables URL中的变量,与Map中的key对应* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {return TEMPLATE.exchange(url, method, requestEntity, responseType, uriVariables);}/*** 通用调用方式** @param url 请求URL* @param method 请求方法类型* @param requestEntity 请求头和请求体封装对象* @param responseBodyType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseBodyType){return TEMPLATE.exchange(url, method, requestEntity, responseBodyType);}/*** 通用调用方式** @param url 请求URL* @param method 请求方法类型* @param requestEntity 请求头和请求体封装对象* @param responseBodyType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseBodyType, Object... uriVariables){return TEMPLATE.exchange(url, method, requestEntity, responseBodyType, uriVariables);}/*** 通用调用方式** @param url 请求URL* @param method 请求方法类型* @param requestEntity 请求头和请求体封装对象* @param responseBodyType 返回对象类型* @return ResponseEntity 响应对象封装类*/public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseBodyType, Map<String, ?> uriVariables){return TEMPLATE.exchange(url, method, requestEntity, responseBodyType, uriVariables);}/*** 获取RestTemplate实例对象,可自由调用其方法* * @return RestTemplate实例对象*/public static RestTemplate getRestTemplate() {return TEMPLATE;}/*** 校验请求是否是200** @param responseEntity* @return*/public static boolean reqSuccess(ResponseEntity<?> responseEntity){return HttpStatus.OK.equals(responseEntity.getStatusCode());}/*** 校验请求是否是200** @param responseEntity* @return*/public static boolean req2xx(ResponseEntity<?> responseEntity){if ( null == responseEntity || null == responseEntity.getStatusCode() ) {return false;}return responseEntity.getStatusCode().is2xxSuccessful();}}
到这里,关于企微创建获客链接时 link_name 中文乱码的情况就处理完了。