1. 关于接口对接加密的问题
    直接上代码了:
    1)ApiRequest
package handler;import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import util.JsonUtil;import java.io.IOException;
import java.util.Map;
import java.util.Objects;public class ApiRequest {private final String url;private final Map<String, String> headerMap;private final String jsonParams;public ApiRequest(String url, Map<String, String> headerMap, String jsonParams) {this.url = url;this.headerMap = headerMap;this.jsonParams = jsonParams;System.out.println("请求header: " + JsonUtil.mapToJson(headerMap));}public String doRequest() {if (null == jsonParams) {return "请求参数为空";}// 通过httpclient构造一个post请求HttpPost httpPost = new HttpPost(url);RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build();httpPost.setConfig(requestConfig);// 设置请求的 headerfor (Map.Entry<String, String> s : headerMap.entrySet()) {httpPost.setHeader(s.getKey(), s.getValue());}// 设置请求数据类型为jsonhttpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");// 设置请求的 bodyHttpEntity entity = new StringEntity(jsonParams, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);try (CloseableHttpClient httpclient = HttpClients.createDefault();CloseableHttpResponse httpResp = httpclient.execute(httpPost)) {if (Objects.nonNull(httpResp)) {return EntityUtils.toString(httpResp.getEntity());}} catch (IOException e) {e.printStackTrace();return e.getMessage();}return "";}
}
  1. ApiService.java
package service;import bo.RespBase;
import bo.req.*;
import bo.resp.*;
import com.fasterxml.jackson.core.type.TypeReference;
import handler.ApiRequest;
import util.Encode;
import util.JsonUtil;
import util.TimeUtil;import java.util.HashMap;
import java.util.Map;public final class ApiService {/*** <b>此处的接口地址需要开发人员自己替换为生产环境的<b/>*/private static final String DOMAIN = "http://s-api.delicloud.com";/*** <b>此处填写实际的 App-Key<b/>*/private static final String APP_KEY = "a34ef2f65bd25fa6f48bbf078a404a73";/*** <b>此处填写实际的 App-Secret<b/>*/private static final String APP_SECRET = "bjyocfomke8itencts06hpnpnq4meg28";private static final String DepartmentURL = "/v2.0/department";private static final String DepartmentQueryURL = "/v2.0/department/query";private static final String DepartmentExtURL = "/v2.0/department/ext";private static final String EmployeeURL = "/v2.0/employee";private static final String EmployeeQueryURL = "/v2.0/employee/query";private static final String EmployeeExtURL = "/v2.0/employee/ext";private static final String CloudAppApiURL = "/v2.0/cloudappapi";private final String domain;public ApiService() {this.domain = DOMAIN;}public ApiService(String domain) {this.domain = domain;}/*** 查询部门信息列表** @param pageReq 分页请求参数* @return 请求结果*/public RespBase<PageResp<DepartmentDataResp>> departmentQuery(PageReq pageReq) {String jsonParams = JsonUtil.objectToJson(pageReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(DepartmentQueryURL);ApiRequest request = new ApiRequest(this.domain + DepartmentQueryURL, headerMap, jsonParams);String body = request.doRequest();RespBase<PageResp<DepartmentDataResp>> respBase = JsonUtil.jsonToObject(body, new TypeReference<RespBase<PageResp<DepartmentDataResp>>>() {});return respBase;}/*** 为部门设置外部ID** @param extReq 请求参数* @return 请求结果*/public RespBase<Void> departmentExt(DepartmentExtReq extReq) {String jsonParams = JsonUtil.objectToJson(extReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(DepartmentExtURL);ApiRequest request = new ApiRequest(this.domain + DepartmentExtURL, headerMap, jsonParams);String body = request.doRequest();RespBase<?> respBase = JsonUtil.jsonToObject(body, RespBase.class);return (RespBase<Void>) respBase;}/*** 添加/修改部门信息** @param departmentCreateReq 请求参数* @return 请求结果*/public RespBase<DepartmentDataResp> departmentRequest(DepartmentCreateReq departmentCreateReq) {String jsonParams = JsonUtil.objectToJson(departmentCreateReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(DepartmentURL);ApiRequest request = new ApiRequest(this.domain + DepartmentURL, headerMap, jsonParams);String body = request.doRequest();RespBase<DepartmentDataResp> respBase = JsonUtil.jsonToObject(body, new TypeReference<RespBase<DepartmentDataResp>>() {});return respBase;}/*** 添加或者编辑员工信息** @param employeeCreateReq 员工信息* @return 请求结果*/public RespBase<EmployeeDataResp> employeeRequest(EmployeeCreateReq employeeCreateReq) {String jsonParams = JsonUtil.objectToJson(employeeCreateReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(EmployeeURL);ApiRequest request = new ApiRequest(this.domain + EmployeeURL, headerMap, jsonParams);String body = request.doRequest();RespBase<EmployeeDataResp> respBase = JsonUtil.jsonToObject(body, new TypeReference<RespBase<EmployeeDataResp>>() {});return respBase;}/*** 批量获取e+平台系统中内的员工信息** @param pageReq 分页查询参数* @return 请求结果*/public RespBase<PageResp<EmployeeBatchDataResp>> employeeQuery(PageReq pageReq) {String jsonParams = JsonUtil.objectToJson(pageReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(EmployeeQueryURL);ApiRequest request = new ApiRequest(this.domain + EmployeeQueryURL, headerMap, jsonParams);String body = request.doRequest();RespBase<PageResp<EmployeeBatchDataResp>> respBase = JsonUtil.jsonToObject(body, new TypeReference<RespBase<PageResp<EmployeeBatchDataResp>>>() {});return respBase;}/*** 为员工设置外部id** @param employeeExtReq 请求参数* @return 请求结果*/public RespBase<Void> employeeExt(EmployeeExtReq employeeExtReq) {String jsonParams = JsonUtil.objectToJson(employeeExtReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(EmployeeExtURL);ApiRequest request = new ApiRequest(this.domain + EmployeeExtURL, headerMap, jsonParams);String body = request.doRequest();RespBase<?> respBase = JsonUtil.jsonToObject(body, RespBase.class);return (RespBase<Void>) respBase;}/*** 批量获取考勤数据** @param checkInReq 请求参数* @return 请求结果*/public RespBase<KqDataResp> cloudAppKqData(CheckInReq checkInReq) {String jsonParams = JsonUtil.objectToJson(checkInReq);System.out.println("请求json: " + jsonParams);// 获取请求头Map<String, String> headerMap = this.getHeaderMap(CloudAppApiURL);headerMap.put("Api-Module", "KQ");headerMap.put("Api-Cmd", "checkin_query");ApiRequest request = new ApiRequest(this.domain + CloudAppApiURL, headerMap, jsonParams);String body = request.doRequest();RespBase<KqDataResp> respBase = JsonUtil.jsonToObject(body, new TypeReference<RespBase<KqDataResp>>() {});return respBase;}/*** 构造请求头** @param requestPath 请求路径* @return 请求头*/private Map<String, String> getHeaderMap(final String requestPath) {// 获取13位时间戳String timeStamp = TimeUtil.getTimestampMills();/* 获取MD5码 */String md5 = Encode.encodeByMD5(requestPath + timeStamp + APP_KEY + APP_SECRET);// 构造请求头参数Map<String, String> headerMap = new HashMap<>(8);headerMap.put("App-Key", APP_KEY);headerMap.put("App-Timestamp", timeStamp);headerMap.put("App-Sig", md5);return headerMap;}
}

3)工具类

package util;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.Map;/*** 序列化工具类*/
public class JsonUtil {private JsonUtil() {}private static final ObjectMapper MAPPER = new ObjectMapper();public static String mapToJson(Map<?, ?> map) {try {return MAPPER.writeValueAsString(map);} catch (JsonProcessingException e) {e.printStackTrace();}return "";}public static <T> T jsonToObject(String json, Class<T> clazz) {try {return MAPPER.readValue(json, clazz);} catch (JsonProcessingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}public static <T> String objectToJson(T data) {try {return MAPPER.writeValueAsString(data);} catch (JsonProcessingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}public static <T> T jsonToObject(String json, TypeReference<T> reference) {try {return MAPPER.readValue(json, reference);} catch (JsonProcessingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}

4)时间工具类

package util;import java.util.Calendar;public class TimeUtil {private TimeUtil() {}/*** 获取时间戳字符串** @return 毫秒时间戳字符串*/public static String getTimestampMills() {return String.valueOf(Calendar.getInstance().getTimeInMillis());}}

5)编码工具类

package util;import java.security.MessageDigest;/*** 编码工具类*/
public class Encode {private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};private static String getFormattedText(byte[] bytes) {int len = bytes.length;StringBuilder buf = new StringBuilder(len * 2);for (byte aByte : bytes) {buf.append(HEX_DIGITS[aByte >> 4 & 15]);buf.append(HEX_DIGITS[aByte & 15]);}return buf.toString();}public static String encode(String algorithm, String str) {if (str == null) {return null;} else {try {MessageDigest messageDigest = MessageDigest.getInstance(algorithm);messageDigest.update(str.getBytes());return getFormattedText(messageDigest.digest());} catch (Exception var3) {throw new RuntimeException(var3);}}}public static String encodeByMD5(String str) {return encode("MD5", str);}
}

先到这里,以后再更

Java版得力API接口文档实现之接口加密相关推荐

  1. Java JDK1.8 API 帮助文档

    Java JDK1.8 API 帮助文档(中文版) 链接:https://pan.baidu.com/s/1bNcjT4_yl6af_dVK3zjaaw 提取码:jbl5

  2. 接口文档编辑工具+接口文档编写

    目录 接口文档编辑工具 接口文档编写 补充 GET与POST的区别 接口文档编辑工具 参考@Lucky锦[接口文档编辑工具] Swagger: 通过固定格式的注释生成文档. 省时省力,不过有点学习成本 ...

  3. 【轻松上手postman】入门篇:如果根据接口文档写postman接口用例

    在我们平时的测试工作中除了最基本的网页测试外,也会遇到没有页面但需要验证内部逻辑正确性的接口测试任务,在遇到没有网页的测试任务时,我们就要使用到接口测试工具来模拟对程序代码触发. 在接到接口测试任务时 ...

  4. java对外发布接口文档_java之接口文档规范

    一.xxxxxx获取指定任务爬取的所有url的接口 接口名称:xxxxxx获取指定任务爬取的所有url的接口 访问链接: http://IP:PORT/crwalTask/findUrlExcepti ...

  5. Java 1.8 API 帮助文档-中文版

    百度云链接: https://pan.baidu.com/s/1mE_O6biq80Z_bCO-ROOWug 密码: m41r

  6. php 接口文档写法,php 接口文档

    接口生成 $config = [ "\\app\\hxbank\\controller\\Hxb", "\\app\\member\\controller\\App&qu ...

  7. 爱迪尔 门锁接口文档_门锁接口说明

    .. 门锁接口说明 **************************************************************** ************************* ...

  8. php的qq接口文档,分账接口

    wx2421b1c4370ec43b 支付测试 10000100 "goods_detail":[ { "goods_id":"iphone6s_16 ...

  9. Swagger3 API接口文档规范课程(Java1234)(内含教学视频+源代码)

    Swagger3 API接口文档规范课程(Java1234)(内含教学视频+源代码) 教学视频+源代码下载链接地址:https://download.csdn.net/download/weixin_ ...

最新文章

  1. Github下载量10万次,最终被所有大厂封杀!
  2. android消息机制
  3. 【面试相关】非计算机专业如何1年内自学拿到算法offer
  4. SQL SERVER 中 GO 的用法2
  5. gta5显示nat较为严格_为何严格治理下雾霾天仍频发?哈尔滨市环保局解答重污染天3大疑问...
  6. c语言 正号运算符 作用,C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,...
  7. oracle脚本导入mysql数据库_oracle脚本导入mysql数据库
  8. Python中缀表达式转后缀表达式并计算
  9. Python中的枚举类型及其用法
  10. 量化风控学习:原来评分卡模型的概率是这么校准的!
  11. 常见的html内lian联元素,CSS基础:块元素、内联元素、内联块元素
  12. 下docfetcher先下Java,docfetcher
  13. Science子刊:母亲的身体气味增强了婴儿和成人的脑-脑同步
  14. 2寸的照片长宽各是多少_两寸照片多少厘米?2寸免冠照片尺寸是多少?2寸免冠照片长宽多少?...
  15. 扎克伯格是如何让员工学会高效工作的?
  16. java isbn_ISBN(国际标准书号)的校验
  17. 项目管理包含了哪些特征?
  18. 大数据影响人类认知和行为习惯
  19. 英飞凌电动汽车参考方案,包含原理图,和Bom清单
  20. 回忆老友蒋新松先生及庆贺《机器人产业发展规划》的发布

热门文章

  1. 怎么用手机修复老照片?手机修复老照片方法分享
  2. 四十四、栅格系统实现(JavaScript原生脚本、媒体查询)
  3. Vue + 项目优化 通过externals加载外部CDN资源
  4. Python3.7安装Scrapy教程
  5. 博士申请 | 香港理工大学王淑君老师招收AI医疗方向全奖博士生/实习生
  6. 转圈报数问题(C语言):有n个人围成一圈,顺序排号……
  7. 图像像素数和分辨率的区别
  8. 冗余-安全设计的基石
  9. python升级pip版本
  10. 云南省计算机一级b类理论知识,计算机一级B类云南省计算机一级考精彩试题库资料...