对于第三方接口调用的模板
1.需要一个基础的Controller,用于读取配置文件里面的信息,比如第三方地址等,还有生成一个HttpPost,用于调用
public class BaseController { /* * appId(对应第三方系统的appId) */ @Value(otherSystem.appId) protected string appId; /* * signId(对应第三方系统的signId */ @Value(otherSystem.signId) protected string signId /* * url(对应第三方系统的url) */ @Value(otherSystem.url) protected string url; protected HttpPost setHeader(String url,String body,String bizMethod) throws MalformedURLException{ //请求路径 HttpPost httpPost = new HttpPost(url); //报文头 httppost.addHeader("Content-Type", "application/json;charset=utf-8"); httppost.addHeader("bizMethod", bizMethod); //需签名加密 httppost.addHeader("appId", appId) ; //加密 httppost.addHeader("signIdn, signId); httppost.addBeader("signMethod","sm3"); //签名方式 String reqTs = new SimpleDateFormat ("yyyyMddHHmmssSSS") .format(new Date ()); httppost.addeadex("reqIs",regIs); //请求方发送报文的时间,unix 时间戳 //加密 String logId = "m3pop" + new SimpleDateFormat ("yyMMddHHmmss5SS").format (new Date()); httppost.addHeadex("requestId", logId); //请求唯一标识加 Stxing origsign = "bizMethod=" + bizMethod + "&" + "appId=" + appld + "&" + "reqTs=" + reqTs + "&" + "requestId" + logId + "&" //sha256计算摘要并转换为16进制 String sign = getSM3Stx(origSign); httppost.addHeadex("sign", sign); return httppost; } protected static String getSM3Str(String str) { if (StringUtils.isBlank(str)){ return null; } String encdestr = ""; try{ bytel] bytes = str.getBytes("UTF-8"); encdeStx = Hex.encodeHexString(SM3Util,sm3 (bytes)); }catch(UnsupportedEncodingException ex){ ex.printstackTrace(); } return encdestr ; }
2.在配置文件里面添加第三方系统的相关配置
#第三方平台
otherSystem:
appId: xxx
signId: xxx
url: xxx
需要注意的是,这些信息不能写死,等写成配置,还有一点就是需要打通你的服务器和第三方之间的网络
3.有了生成HttpPost的基础类之后,我们可以写一个业务类去继承它,在里面去封装一些需要调用接口用到的信息
@Api("第三方平台品牌业务”)
@Slf4j
@RestController
@RequestMapping("/corporater")
public class BrandManageController extends BaseController {
/*
* 品牌列表查询
*/
@ApiOperation(value ="品牌列表查询)
@PostMapping("/brandlist")
public DataResponse<BrandInfoVo> queryBrandList(@RequestBody BrandListParam param) {
DataResponse<BrandInfoVo> dataResponse=new DataResponse<>();
String resultData = null;
BrandInfovo brandInfoVo=new BrandInfovo();
try (CloseableHttpClient httpclient = HttpClients.createDefault()){
//封装请求信息
JSONObject bodyjson = new JSONObject();
bodyjson .put (uversion""1.0.0");
bodyjson .put ("pageNum",param .getPageNum());
bodyjson.put ("brandId", param.getBrandId());
bodyjson.put("brandlias", param.getBrandAlias());
bodyison.put("srcBrandId", param.getSrcBrandId());
bodyjson.put("bussCatId",param,getBussCat2Id());
bodyjson.put("bussCatIdGroup", param,getBussCatiId()):
bodyison .put ("brandNm", param,getBrandNm());
bodyjson,put("procSt","8");
String body = bodyjson.tostring();
HttpPost httppost =
setHeader(ssourl+"/corporate/brandlist",body,"corporate.brandlist");
httppost.setEntity(new StringEntity(body,"UTF-8"));
log.info("第三方品牌列表查询请求报文:{}",body):
CloseableHttpResponse response = httpclient.execute (httppost);
resultData = EntityUtils.toString(response.getEntity());
log.info("第三方品牌列表查询返回数据:()",resultData);
brandInfoVo = JSON.parseObject (resultData,BrandInfoVo.class);
}catch(Exception e){
log.error(e.getMessage(), e);
log.error("品牌列表查询:(),e.getMessage());
return new DataResponse<>(ExceptionDefinition.FuseError, brandInfoVo);
}
return new DataResponse<>(ExceptionDefinition.OK,brandInfoVo);
}
注意:要在调用接口前后加上日志,以便后续出问题好排查
浙公网安备 33010602011771号