共计 3893 个字符,预计需要花费 10 分钟才能阅读完成。
1. 设置 RestTemplate 申请工具
在 common 文件夹下创立 HTTPRequestRes.java(个别通用的工具或办法都创立在海星的 common 文件夹下)
在该文件内设置 RestTemplate 申请工具
代码如下:
package com.hikvision.isc.common.utils;
import com.hikvision.starfish.core.response.business.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
@Component
public class HTTPRequestRes {private static final Logger logger = LoggerFactory.getLogger(HTTPRequestRes.class);
public static final String ACCEPT = "Accept";
public static final String CONTENT_ENCODING = "Content-Encoding";
public static final String CONTENT_TYPE = "Content-Type";
private static BaseResponse baseResponse = new BaseResponse();
@Autowired
private RestTemplate restTemplate;
public <T> BaseResponse RestTemplate(String userId, String url, HttpMethod httpMethod, Object content, Class<T> t) throws Exception {if (StringUtils.isEmpty(url)) {return baseResponse;}
HttpHeaders headers = getHttpHeaders(userId);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<>(content, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, httpMethod, entity, String.class);
String ret = responseEntity.getBody();
baseResponse = JSONObject.parseObject(ret, BaseResponse.class);
return baseResponse;
}
public static HttpHeaders getHttpHeaders(String userId) {HttpHeaders headers = new HttpHeaders();
headers.add(ACCEPT, "application/json");
headers.add(CONTENT_ENCODING, "UTF-8");
headers.add(CONTENT_TYPE, "application/json; charset=UTF-8");
headers.add("userId",userId);
return headers;
}
}
2. 调用 RestTemplate 申请工具申请 irds 组件的相干接口
以 irds 组件的以下接口为例:
(1)在 service.impl 文件夹下创立任意文件
在 service.impl 文件夹下创立 MyIrdsService,
在代码中自定义办法,设置参数,通过调用 RestTemplate 申请办法进行申请
package com.hikvision.isc.module.business.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.isc.common.utils.HTTPRequestRes;
import com.hikvision.starfish.core.response.business.BaseResponse;
import com.hikvision.starfish.irds.response.PageData;
import com.hikvision.starfish.irds.service.IIrdsService;
import com.hikvision.starfish.irds.vo.RegionQueryReqVo;
import com.hikvision.starfish.irds.vo.RegionVo;
import com.hikvision.starfish.irds.vo.SubRegionQueryReqVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.hikvision.isc.common.errorcode.DefaultErrorCode;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MyIrdsService {private static Logger logger = LoggerFactory.getLogger(MyIrdsService.class);
private static final String IRDS = "irds";
private static final String IRDS_CAMERA_LIST = "/irds/api/v1/camera/advance/cameraList";
@Autowired
private HTTPRequestRes httpRequestRes;
/** 依据监控点名称获取列表 */
public BaseResponse queryCameraListByName(String cameraName, String userId){BaseResponse baseResponse = new BaseResponse();
// 拼接残缺的相应接口的残缺申请地址
String url = "http://" + IRDS + "." + IRDS + IRDS_CAMERA_LIST;
try{
// 在这里设置接口的申请参数
JSONObject jsonObject = new JSONObject();
jsonObject.put("pageNo", 1); // 起始页
jsonObject.put("pageSize", 1000); // 指定每页的记录数据量大小
jsonObject.put("cameraName", cameraName); // 监控点名称
jsonObject.put("isCascade", 2); // 是否级联, 0:非级联, 1:级联, 2:不限
//userId: 用户名 url:接口申请地址 HttpMethod.POST:申请形式(能够为 GET、POST、PUT 申请等)jsonObject:申请参数
// 调用 RestTemplate 申请工具,传入相应参数进行申请
baseResponse = httpRequestRes.RestTemplate(userId, url, HttpMethod.POST, jsonObject, JSONObject.class);
return baseResponse;
}catch (Exception e){baseResponse = exection(url + e.getMessage());
return baseResponse;
}
}
public BaseResponse exection(String message) {BaseResponse baseResponse = new BaseResponse();
baseResponse.setCode("-1");
baseResponse.setMsg(message);
logger.error("irds 拜访失败", message);
return baseResponse;
}
}
3. 定义 service 接口以及接口实现类
在 service 中定义接口
在接口实现类中重写接口中的办法,调用 2 中的办法即可
4. 定义 controller 层
5. 进行测试
调用胜利,胜利从 irds 组件取得指定的数据
正文完