wiki-files/wg-basic/remote-rest.md

73 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 8.REST远程调用工具
description: REST远程调用工具
published: true
date: 2021-09-20T09:22:59.074Z
tags: wg-basic, remote
editor: markdown
dateCreated: 2021-09-20T08:24:01.498Z
---
# 功能说明
为了能够在分布式系统中更加方便高效的发送 Restful 请求,在 **common** 基础包中添加了 **远程调用工具**。该工具对 RestTemplate 进行了代理封装,开发人员只需要编写用于远程调用的接口,完成依赖注入即可。
*注:该工具使用方式类似 @Controller*
# 注解
> 远程调用的Inerface需要在 `remote` 目录下,在其余位置无法被扫描
1. 接口注解
|名称|参数|位置|说明|示例|
|-|-|-|-|-|
|@RemoteService|-|接口|标注接口,带有此注解的接口才能被自动注入|-|
2. 方法注解
> 请求的路径中可以包含 **路径参数**,通过参数注解 `@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}") |
3. 参数注解
|名称|参数|位置|说明|示例|
|-|-|-|-|-|
|@RemoteHeaderParams|header内容|参数|发送请求时会将参数自动添加到header中一颗有多个如果重复以最后一个为准| `@RemoteHeaderParams("h1") String h1` |
|@RemotePathParams|path参数|参数|路径中的参数值,数量应与路径中变量一致,不能有重复值,请求时会自动替换| `@RemotePathParams("p1") String p1` |
|@RemoteQueryParams|请求参数|参数|请求时的参数,可以有多个,重复以最后一个为准| `@RemotePathParams("q1") String q1` |
|@RemoteServerParams|服务地址|参数|该参数 **必填**需以http或https开头| `@RemoteServerParams String server` |
|@RemoteJsonBodyParams|Json对象|参数|该参数在POST或PUT请求时必填类型为Object| `@RemoteJsonBodyParams User user` |
# 示例
```java
@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);
}
```