http工具类统一封装
个人封装使用,仅供参考,用来捕获接口异常,并且通知到企业微信
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package com.tthk.inland.ticket.core.utils.https;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import com.tthk.framework.base.api.dto.CommonResult; import com.tthk.inland.ticket.core.enums.result.ResulEnum; import com.tthk.inland.ticket.core.enums.wechat.WeChatEnums; import com.tthk.inland.ticket.core.utils.wechat.WeChatRobotUtils; import com.xxl.job.core.context.XxlJobHelper;
import java.lang.reflect.Type;
public class RequestStatusNotify {
public static <T> CommonResult<T> requestStatusNotify(HttpRequest request,Class<? extends T> aClass,Type type){ final var execute = request.execute(); if(execute.getStatus() != 200){ XxlJobHelper.log("接口调用失败"); WeChatRobotUtils.pushNotice("【"+execute.getStatus()+"】接口调用失败:"+request.getUrl(), WeChatEnums.WARN); return CommonResult.error(ResulEnum.INTERFACE_FALT.getErrorCode()); }else { T o = null; if(aClass != null){ o = JSONObject.parseObject(execute.body(), aClass); } if(type != null){ o = JSONObject.parseObject(execute.body(), type); } XxlJobHelper.log(execute.getStatus()+"接口调用结果:"+JSONObject.toJSONString(o)); return CommonResult.success(o); } }
public static <T> CommonResult<T> requestStatusNotify(HttpRequest request,Type type){ return requestStatusNotify(request,null,type); }
public static <T> CommonResult<T> requestStatusNotify(HttpRequest request,Class<? extends T> aClass){ return requestStatusNotify(request,aClass,null); } }
|
使用方法
1 2 3 4 5
| public static final String SPECIAL_PRICE_URL = APP_OPER_URL + "/AppOper/GetPrice"; public static CommonResult<B2CSpecialPriceVO> getB2CSpecialPrice(B2CSpecialPriceDTO b2CSpecialPriceDTO){ final var body = HttpRequest.get(SPECIAL_PRICE_URL).form(EntityUtils.entityToMap(b2CSpecialPriceDTO)); return RequestStatusNotify.requestStatusNotify(body, new TypeReference<CommonResult<B2CSpecialPriceVO>>(){}.getType()); }
|