第三方接口调用时,根据接口文档封装请求类和接收类,定义一个公用方法发送post请求。
- baseReq类:
package com.wy.interfacecall.apiReq;
import lombok.Data;
import java.io.Serializable;
/**
* base请求类
*
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 09:56
*/
@Data
public class BaseReq implements Serializable {
// 公用请求字段
private String id;
private String uCode;
private String sign;
private Long timeStamp;
public String getReqPath() {
return null;
}
}
- baseResp类:
package com.wy.interfacecall.apiReq;
import lombok.Data;
import java.io.Serializable;
/**
* base响应类
*
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 10:01
*/
@Data
public class BaseResp implements Serializable {
// 响应码
private Integer code;
// 响应信息
private String msg;
}
- 请求类:
package com.wy.interfacecall.apiReq;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 获取商品请求封装类
*
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 10:03
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GetProductReq extends BaseReq {
public static final String REQ_URL = "xxxxxx";
// 页数
private Integer page;
// 每页记录数
private Integer rows;
// 创建时间
private String createTime;
@Override
public String getReqPath() {
return GetProductReq.REQ_URL;
}
// 印象中,无参构造上方加 @Tolerate注解可以解决 @Builder和 @Data共用时没有无参方法的问题
// @Tolerate
// public GetProductReq() {
// }
}
- 响应类:
package com.wy.interfacecall.apiReq;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
/**
* 获取商品响应类
*
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 10:10
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GetProductResp extends BaseResp {
// 对应第三方接口返回的数据形式
@JSONField(name = "data")
private List<Product> productList;
@Data
public static class Product implements Serializable {
private String productId;
private String productName;
private Double productPrice;
}
}
- 接口定义:
package com.wy.interfacecall.service;
import com.wy.interfacecall.apiReq.BaseReq;
import com.wy.interfacecall.apiReq.BaseResp;
import com.wy.interfacecall.apiReq.GetProductResp;
/**
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 10:19
*/
public interface ApiService {
// api公用请求接口
BaseResp legacyApiInvoker(BaseReq baseReq, BaseResp baseResp);
// 获取商品接口(传入请求参数,实现类中build请求对象)
GetProductResp getProduct(Integer page, Integer rows, String createTime);
}
- 实现类:
package com.wy.interfacecall.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.wy.interfacecall.apiConfig.ApiConfig;
import com.wy.interfacecall.apiReq.BaseReq;
import com.wy.interfacecall.apiReq.BaseResp;
import com.wy.interfacecall.apiReq.GetProductReq;
import com.wy.interfacecall.apiReq.GetProductResp;
import com.wy.interfacecall.service.ApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
* 实现类
*
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 10:22
*/
@Slf4j
public class ApiServiceImpl implements ApiService {
@Autowired
private ApiConfig apiConfig;
@Override
public BaseResp legacyApiInvoker(BaseReq req, BaseResp resp) {
req.setId(apiConfig.getId());
req.setUCode(apiConfig.getUCode());
req.setTimeStamp(DateUtil.currentSeconds());
Map<String, Object> paramMap = new TreeMap<>();
// 请求对象转为map
paramMap = BeanUtil.beanToMap(req, paramMap, false, true);
// BasicNameValuePair通常是用来封装post请求中的参数名称和值,且name和value必须为String类型
List<BasicNameValuePair> params = paramMap.entrySet().stream().map(item -> new BasicNameValuePair(item.getKey(), item.getValue().toString())).collect(Collectors.toList());
// MD5加密后转大写
String signStr = DigestUtil.md5Hex(URLUtil.decode(URLEncodedUtils.format(params, "utf-8"))).toUpperCase();
paramMap.put("sign", signStr);
log.info("请求路径:{},请求参数:{}", req.getReqPath(), paramMap);
// 发送请求
String result = HttpUtil.post(apiConfig.getBaseAPIURL() + req.getReqPath(), paramMap);
log.info("响应结果:{}", result);
return JSON.parseObject(result, resp.getClass());
}
@Override
public GetProductResp getProduct(Integer page, Integer rows, String createTime) {
// build出一个请求对象
GetProductReq req = GetProductReq.builder().page(page).rows(rows).createTime(createTime).build();
// 调用方法发送请求
return (GetProductResp) legacyApiInvoker(req, new GetProductResp());
}
}
- 配置类:
package com.wy.interfacecall.apiConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 配置类
*
* @author wangyan@163.com
* @version 1.0
* @date 2019-12-30 10:23
*/
@Data
@Component
@ConfigurationProperties(prefix = "xxx.api")// 对应yml或者properties文件中配置
public class ApiConfig {
// 请求域名
private String baseAPIURL;
/**
* 商户ID
*/
private String id;
/**
* 接入码
*/
private String uCode;
private String appKey;
private String secret;
}
- yml配置形式:
xxx:
api:
baseAPIURL: ${API_URL:http://xxxxxxx}
id: ${API_ID:xxx}
uCode: ${API_UCODE:xxx}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115790.html