背景叙述
一个本地客户端(就是exe安装包)的开发,因此会复用一些web端系统的接口,在复用时肯定就是拿到返回结果后的处理了,因为web端系统的返回结果原本是给前端用的,一般都是JSON(JSONObject)对象,因此在客户端拿到返回结果后需要对数据进行处理,下面就针对某个示例进行记录;
示例:
传入一个value值,去web端系统中查询这个value值在字典中的中文名称是什么;
业务逻辑代码
public String getNameByValue(String value) {
User currentUser = UserUtil.getCurrentUser();
if (StringUtils.isNotBlank(currentUser.getUser_id())) {
// 开始拼装请求
String url = SYS_BASE_URL + "/dict/dictionary?code=blockType";
restTemplate.getInterceptors().add(interceptor);
String result = restTemplate.getForObject(url, String.class);
// 设置接收参数的数据模型
ResponseData<List<BlockTypeInfo>> resourceData = FastJsonUtil.toBean(result, ResponseData.class);
if (resourceData.getHttpCode() == 200) {
// 首先转化成List<BlockTypeInfo>的数据结构 - 但是里面的对象数据仍然是JSONObject类型的,不可被正常操作
List<BlockTypeInfo> blockTypeInfoList = FastJsonUtil.toBean(JSON.toJSONString(resourceData.getData()), List.class);
// 循环List列表中的元素,将JSONObject类型BlockTypeInfo数据进一步转化为真正的BlockTypeInfo对象
for (int i = 0; i < blockTypeInfoList.size