对接世邦XC-9000 HTTP
controller
/**
* 获取设备信息
*/
@GetMapping("/sb/dev")
@ResponseBody
public R devInfo() {
// 世邦ip 端口配置
DevInfo sb = cfg.getShibang();
ZoneDevInfoParam zoneterminalParam = new ZoneDevInfoParam();
zoneterminalParam.setIp(sb.getIp());
zoneterminalParam.setPort(sb.getPort());
zoneterminalParam.setGroupName("*");
ApiZoneDevInfoResult zoneterminalTask = shiBangXC9000ApiService.getZoneterminalTask(zoneterminalParam);
return R.okData(zoneterminalTask);
}
serviceImpl
@Override
@ContractApiUrl(value = "/php/getzoneterminaldata.php")
public ApiZoneDevInfoResult getZoneterminalTask(ZoneDevInfoParam param) {
String url = getPostUrl(ZoneDevInfoParam.class);
url = URL.replace(IP, param.getIp()).replace(PORT, param.getPort() + "") + url;
ApiZoneDevInfoResult ret = PostSBReqUtils.Post(url, param, ApiZoneDevInfoResult.class);
return ret;
}
/**
* 获取第三方对接地址
*
* @return 请求URL
*/
private String getPostUrl(Class<?>... parameterTypes) {
try {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
String methodName = stack[2].getMethodName();
Method method = this.getClass().getMethod(methodName, parameterTypes);
ContractApiUrl apiUrl = method.getAnnotation(ContractApiUrl.class);
if (apiUrl != null) {
return apiUrl.value();
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
PostSBReqUtils
@Slf4j
@Component
public class PostSBReqUtils {
public static <T> T Post(String reqPath, Object obj, Class<T> clazz) {
try {
T temp = clazz.newInstance();
String json = getJson(obj);
Map<String, Object> param = getMapFormJson(json);
String resp = HttpSBUtil.doPost(reqPath, param);
System.err.println(reqPath + " === > " + resp);
T ret = JsonUtil.fromJson(resp, clazz);
return null == ret ? temp : ret;
} catch (Exception e) {
e.printStackTrace();
log.error("PostItcReqUtils.Post===>" + reqPath + " " + JsonUtil.toJson(obj));
return null;
}
}
private static String getJson(Object obj) {
return JsonUtil.toJson(obj);
}
@SuppressWarnings("unchecked")
private static Map<String, Object> getMapFormJson(String obj) {
Map<String, Object> param = new HashMap<String, Object>();
try {
param = JsonUtil.fromJson(obj, Map.class);
} catch (Exception e) {
}
return param;
}
}