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. (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
  2. Tomcat中配置MySQL数据库连接池
  3. python gui插件_Python进阶量化交易专栏场外篇17- GUI控件在回测工具上的添加
  4. boost::remove_edge_if用法的测试程序
  5. mybatis-plus 查询,删除
  6. 698. Partition to K Equal Sum Subsets
  7. 简述Struts2 Convention零配置
  8. JDK源码(10)-Integer(用处最多,重点讲解)
  9. mysql 判断 字母大写_MySQL中查询时对字母大小写的区分
  10. matlab2012 powerlib,matlab没有powerlib2
  11. python编程入门-最好的Python入门教材是哪本?
  12. 计算机房消防设计规范,发电机房消防设计规范要求有哪些
  13. GridView边框样式简单美化
  14. SPSS——描述性统计分析——比率分析
  15. CMP是什么意思?谁能解释下?
  16. 大话数据结构——烂笔头
  17. sPortfolio: Stratified Visual Analysis of Stock Portfolios
  18. ES官网reference翻译文章(18)—Percentile Ranks Aggregation
  19. Receptive Field Block Net for Accurate and Fast Object Detection(RFB)
  20. 基于Sketch Up软件校园建模案例分享

热门文章

  1. extjs资源库管理平台 2013.6.15-电子书库
  2. python 开立方注意事项
  3. 使用docker启动rabbitmq
  4. 编写通讯录(文件版)
  5. 机器学习第十三章——支持向量机
  6. 这些前端必备的硬核插件库,你都get了吗?
  7. python里的jango类视图
  8. flutter学习(3)图片组件
  9. 内容中的img标签正则成mip-img
  10. linux中socket的理解