远程调用的项目使用PHP写的,因此我只能通过页面的请求参数封装请求Vo,然后调用PHP项目的接口封装响应类。
① 请求体:Feign接口中请求体的字段要比调用的PHP项目中接口的请求体字段少
@Data
public class AssetListQo {
@JsonProperty(value = "time_range")
private String timeRange;
@JsonProperty(value = "branch_id")
private String branchId;
private Integer start;
private Integer limit;
@JsonProperty(value = "need_risk_tags")
private Boolean needRiskTags;
@JsonProperty(value = "task_id")
private String taskId;
}
② OpenFiegn接口
@FeignClient(url = "${proxy.base_url}", name = "${apps.asset.registry_name}")
public interface AssetFeignClient {
@RequestMapping(value = "${apps.asset.urls.asset.list}", method = RequestMethod.POST)
AssetApiResponse<List<AssetListVo>> getAssetList(@RequestBody AssetListQo assetListQo);
}
③ application.yml文件:
apps:
asset:
registry_name: Asset
urls:
asset:
list: /apps/asset/asset_view/on_list
④ 远程调用PHP项目的/apps/asset/asset_view/on_list接口,其响应结果为:
{
"success": true,
"data": [
{
"id": 7,
"asset_name": "test2",
"host_name": "",
"name": "1.0.0.3",
"mac": "",
"ip": "1.0.0.3",
"is_dynamic": false,
"status": "online",
"magnitude": "normal",
"classify1_id": 1,
"classify_id": 10000,
"classify_name": "服务器",
"tags": [],
"protocol_port": [],
"system": "",
"first_time": "2022-04-15 11:06:39",
"last_time": "2022-04-15 11:06:39",
"find_type": "manually",
"branch_id": 0,
"branch_name": "-",
"user": [],
"users": [],
"source_device": [],
"dev_type": [],
"service": [],
"comment": "",
"domain_name": "",
"location": {
"country": "中国",
"country_id": 737014929,
"province": "",
"province_id": 1744973543,
"city": "",
"city_id": 1744973543
},
"is_down_asset": false,
"system_name": "",
"department": "-",
"uses": "",
"business_name": "",
"risk_tags": []
},
{
"id": 6,
"asset_name": "test1",
"host_name": "",
"name": "1.0.0.1",
"mac": "",
"ip": "1.0.0.1",
"is_dynamic": false,
"status": "online",
"magnitude": "normal",
"classify1_id": 1,
"classify_id": 10000,
"classify_name": "服务器",
"tags": [],
"protocol_port": [],
"system": "",
"first_time": "2022-04-15 11:06:23",
"last_time": "2022-04-15 11:06:23",
"find_type": "manually",
"branch_id": 0,
"branch_name": "-",
"user": [],
"users": [],
"source_device": [],
"dev_type": [],
"service": [],
"comment": "",
"domain_name": "",
"location": {
"country": "中国",
"country_id": 737014929,
"province": "",
"province_id": 1744973543,
"city": "",
"city_id": 1744973543
"latitude": ""
},
"is_down_asset": false,
"system_name": "",
"department": "-",
"uses": "",
"business_name": "",
"risk_tags": []
}
],
"total": 2
}
⑤ Feign接口的响应类为:可以看到OPenFeign接口响应类比PHP项目中接口的响应类字段要少;
@Data
public class AssetApiResponse<T> {
private Boolean success;
private Integer total;
private T data;
}
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class AssetListVo {
private Integer id;
@JsonProperty(value = "asset_name")
private String assetName;
@JsonProperty(value = "host_name")
private String hostName;
private String name;
private String ip;
private String status;
@JsonProperty(value = "branch_id")
private Integer branchId;
@JsonProperty(value = "branch_name")
private String branchName;
}
注意:
① Feign接口中请求类的字段可以比远程调用接口的请求类字段少;
② Feign接口中响应类的字段可以比远程调用接口的响应类字段少;
③ 当响应类封装的不对时,先将远程调用的响应类型设置为String,观察响应结果;
解决过程:
@FeignClient(url = "${proxy.base_url}", name = "${apps.asset.registry_name}")
public interface AssetFeignClient {
@RequestMapping(value = "${apps.asset.urls.asset.list}", method = RequestMethod.POST)
String getAssetList(@RequestBody AssetListQo assetListQo);
}
先把Fiegn接口中PHP项目中接口的响应类型设置为String,拿到响应结果,然后根据响应结果封装响应类。
使用Feign接口:
@Override
public List<AssetListVo> assetList(AssetListQo assetListQo) {
AssetApiResponse<List<AssetListVo>> assetList = assetFeignClient.getAssetList(assetListQo);
return assetList.getData();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)