4.0 KiB
4.0 KiB
title | description | published | date | tags | editor | dateCreated |
---|---|---|---|---|---|---|
8.REST远程调用工具 | REST远程调用工具 | true | 2021-09-24T06:17:28.930Z | wg-basic, remote | markdown | 2021-09-20T08:24:01.498Z |
功能说明
为了能够在分布式系统中更加方便高效的发送 Restful 请求,在 common 基础包中添加了 远程调用工具。该工具对 RestTemplate 进行了代理封装,开发人员只需要编写用于远程调用的接口,完成依赖注入即可。
注:该工具使用方式类似 @Controller
注解
远程调用的Inerface,需要在
remote
目录下,在其余位置无法被扫描
- 接口注解
名称 | 参数 | 位置 | 说明 | 示例 |
---|---|---|---|---|
@RemoteService | - | 接口 | 标注接口,带有此注解的接口才能被自动注入 | - |
- 方法注解
请求的路径中可以包含 路径参数,通过参数注解
@RemotePathParams
替换
名称 | 参数 | 位置 | 说明 | 示例 |
---|---|---|---|---|
@RemoteGetMethod | 请求的地址 | 方法 | 调用该接口时,发送 Get 请求 | @RemoteGetMethod("/api/p1/{params1}") |
@RemoteDeleteMethod | 请求的地址 | 方法 | 调用该接口时,发送 DELETE 请求 | @RemoteDeleteMethod("/api/p1/{params1}") |
@RemotePostMethod | 请求的地址 | 方法 | 调用该接口时,发送 POST 请求 | @RemotePostMethod("/api/p1/{params1}") |
@RemotePutMethod | 请求的地址 | 方法 | 调用该接口时,发送 PUT 请求 | @RemotePutMethod("/api/p1/{params1}") |
- 参数注解
名称 | 参数 | 位置 | 说明 | 示例 |
---|---|---|---|---|
@RemoteHeaderParams | header内容 | 参数 | 发送请求时,会将参数自动添加到header中,一颗有多个,如果重复以最后一个为准 | @RemoteHeaderParams("h1") String h1 |
@RemotePathParams | path参数 | 参数 | 路径中的参数值,数量应与路径中变量一致,不能有重复值,请求时会自动替换 | @RemotePathParams("p1") String p1 |
@RemoteQueryParams | 请求参数 | 参数 | 请求时的参数,可以有多个,重复以最后一个为准 | @RemotePathParams("q1") String q1 |
@RemoteQueryParamsMap | query参数集合 | 参数 | 请求时参数的HashMap集合,重复以最后一个为准,该参数可与 @RemoteQueryParams 共同使用,也可以单独使用,相当于 @RemoteQueryParams 参数集合 | @RemoteQueryParamsMap Map<String, Object> params |
@RemoteServerParams | 服务地址 | 参数 | 该参数 必填,需以http或https开头 | @RemoteServerParams String server |
@RemoteFormParams | 表单参数 | 参数 | POST提交表单,可以有多个,与 @RemoteJsonBodyParams 不能一起使用 | @RemoteServerParams String server |
@RemoteJsonBodyParams | Json对象 | 参数 | 该参数在POST或PUT请求时必填,类型为Object | @RemoteJsonBodyParams User user |
示例
@RemoteService
public interface IDemo {
@RemoteGetMethod("/app/user-expand/get-user-release/{userId}")
UserDTO getUser(@RemoteServerParams String remotePath, @RemotePathParams("userId") String userId);
@RemoteGetMethod("/app/user-expand/list-user-release")
List<UserDTO> listUser(@RemoteServerParams String remotePath);
@RemoteGetMethod("/app/user-expand/listpage-user-release")
SuccessResultList<List<UserDTO>> listPageUser(@RemoteServerParams String remotePath, @RemoteQueryParams("page") Integer page, @RemoteQueryParams("size") Integer size);
@RemotePostMethod("/app/user-expand/save-release")
SuccessResult postRelease(@RemoteServerParams String remotePath, @RemoteJsonBodyParams Map<String, Object> params);
@RemotePostMethod("/app/user-expand/save-list-release")
SuccessResult postListRelease(@RemoteServerParams String remotePath, @RemoteJsonBodyParams List<Map<String, Object>> params);
@RemotePutMethod("/app/user-expand/update-release")
SuccessResult putRelease(@RemoteServerParams String remotePath, @RemoteJsonBodyParams Map<String, Object> params);
@RemoteDeleteMethod("/app/user-expand/delete-release/{userIds}")
SuccessResult deleteRelease(@RemoteServerParams String remotePath, @RemotePathParams("userIds") String userIds);
}