后端华为OpenApis开发
# openapi模版说明
# 1. 设计背景
- 规范化不规范的华为接口。
# 2. API定义说明
# 2.1 参数说明
public static HuaweiOpenApi<artListApiDTO, PartBatchViewApiDTO> partsBatchDetailWrapper = new HuaweiOpenApi<>(
"/imes/mdmService/openapi/v1/parts/batch-detail/wrapper?limit={pageSize}&offset={pageNum}",
"API接口说明", "POST", new TypeReference<OpenApiPageWrapperVO<PartBatchViewApiDTO>>(){},
RequestPolicy.CAMEL_2_UNDERLINE_POLICY, ResponsePolicy.UNDERLINE_2_CAMEL_POLICY);
1
2
3
4
2
3
4
说明:
| 说明 | ||
|---|---|---|
| <PartListApiDTO, PartBatchViewApiDTO> | PartListApiDTO 是接口的请求数据的对象格式; PartBatchViewApiDTO 是接口返回数据的非包装类的对象格式 | |
| /imes/mdmService/openapi/v1/parts/batch-detail/wrapper?limit={pageSize}&offset={pageNum} | 接口的URL, 如果需要使用url传参,使用{XXX}等方式的占位符传参。 | |
| new TypeReference<OpenApiPageWrapperVO<PartBatchViewApiDTO>>(){} | 接口返回数据 data 字段的对象定义。可能是列表,分页对象,普通对象,基础类型对象 | |
| RequestPolicy.CAMEL_2_UNDERLINE_POLICY | 请求数据策略: 需要把对象的字段从驼峰转成下划线 | |
| ResponsePolicy.UNDERLINE_2_CAMEL_POLICY | 返回数据策略: 需要把对象的字段从下划线转成驼峰 |
泛型定义 与 TypeReference 补充说明:
通用返回结构:
{
"data": {
"count": 1,
"limit": 1,
"offset": 0,
"data": [{
"rdmExtensionType": "Part"
}]
},
"message": {
"msg": "success"
},
"status": 200
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| 返回格式 | 返回data格式TypeReference定义 | 泛型定义 |
|---|---|---|
| {"data": { "tenant_id": "hwstaff_p_apsimes", "created_by": "zhangyingfeng"}} | new TypeReference<PartBatchViewApiDTO>(){} | Api<PartListApiDTO, PartBatchViewApiDTO> |
| {"data": [{ "rdmExtensionType": "Part",}]} | new TypeReference<List<PartBatchViewApiDTO>>(){} | Api<PartListApiDTO, PartBatchViewApiDTO> |
| {"data": 999} | new TypeReference<Integer>(){} | |
| {"data": null} | new TypeReference<Void>(){} | Api<PartListApiDTO, Void> |
| {"data": { "count": 1, "limit": 1, "offset": 0, "data": [{ "rdmExtensionType": "Part" }]},"message": { "msg": "success"},"status": 200} | new TypeReference<OpenApiPageWrapperVO<PartBatchViewApiDTO>>(){} | Api<PartListApiDTO, PartBatchViewApiDTO> |
# 3. API使用方式
分为两种类型:1. 提交类型接口; 2. 查询类型接口
public interface IOpsTemplateApi<K,V> {
// ----------------- 提交类型接口 ------------- //
/**
* 提交类型接口(新增, 更新,删除等功能)
*
* @author 戴志强
* {@code @date} 2025/5/8 14:10
* @return 提交接口返回值
*/
@Nullable
V submit() throws DelegateException;
/**
* 提交类型接口(新增, 更新,删除等功能)
* 提交数据并返回一个列表
*
* @author 戴志强
* {@code @date} 2025/5/8 14:10
* @return List列表
*/
List<V> submitGetList() throws DelegateException;
// ----------------- 分页类型接口(带OpenApiPageWrapperVO) ------------- //
/**
* 分页查询接口
*
* @author 戴志强
* {@code @date} 2025/5/8 14:10
* @param pageVO 分页信息
* @return 分页查询返回值
*/
PageResultVO<V> page(PageApiVO pageVO) throws DelegateException;
/**
* 调用分页查询接口获取第一条数据
*
* @author 戴志强
* {@code @date} 2025/5/8 14:10
* @return 分页查询返回值
*/
@Nullable
V pageGetOne() throws DelegateException;
/**
* 多次调用分页查询接口获取所有数据
*
* @author 戴志强
* {@code @date} 2025/5/8 14:10
* @return 分页查询返回值
*/
List<V> pageGetAll() throws DelegateException;
/**
* 分页查询接口
*
* @author 戴志强
* {@code @date} 2025/5/8 14:10
* @param pageVO 分页信息
* @return List列表
*/
List<V> pageList(@Nullable PageApiVO pageVO) throws DelegateException;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
提交类型接口(新增,删除,更新等):
// ---------- 接口定义 ------------ //
public static HuaweiOpenApi<PartListApiDTO, Integer> partsBatchDetailInteger = new HuaweiOpenApi<>(
"/imes/mdmService/openapi/v1/parts/batch-detail/integer?limit={pageSize}&offset={pageNum}",
"API接口说明", "POST", new TypeReference<Integer>(){},
RequestPolicy.CAMEL_2_UNDERLINE_POLICY, ResponsePolicy.UNDERLINE_2_CAMEL_POLICY);
// ---------- 接口使用 ------------ //
PartListApiDTO partListApiDTO = new PartListApiDTO();
partListApiDTO.setPartIdList(Collections.singletonList("partId001"));
Integer responseSubmit = PartOpenApis.partsBatchDetailInteger.ops(partListApiDTO).submit();
System.out.println("Response Body responseSubmit: " + JSON.toJSONString(responseSubmit));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
查询类型接口:
// ---------- 接口定义 ------------ //
public static HuaweiOpenApi<PartListApiDTO, PartBatchViewApiDTO> partsBatchDetailWrapper = new HuaweiOpenApi<>(
"/imes/mdmService/openapi/v1/parts/batch-detail/wrapper?limit={pageSize}&offset={pageNum}",
"API接口说明", "POST", new TypeReference<OpenApiPageWrapperVO<PartBatchViewApiDTO>>(){},
RequestPolicy.CAMEL_2_UNDERLINE_POLICY, ResponsePolicy.UNDERLINE_2_CAMEL_POLICY);
// ---------- 接口使用 ------------ //
PartListApiDTO partListApiDTO = new PartListApiDTO();
partListApiDTO.setPartIdList(Collections.singletonList("partId001"));
PageApiVO pageApiVO = new PageApiVO();
pageApiVO.setCurrent(1);
pageApiVO.setPageSize(200);
PageResultVO<PartBatchViewApiDTO> responsePage = PartOpenApis.partsBatchDetailWrapper.ops(partListApiDTO).page(pageApiVO);
// --------- pageGetOne接口测试 ---------- //
PartBatchViewApiDTO responseGetOne = PartOpenApis.partsBatchDetailWrapper.ops(partListApiDTO).pageGetOne();
// --------- pageGetAll接口测试 ---------- //
List<PartBatchViewApiDTO> responseGetAll = PartOpenApis.partsBatchDetailWrapper.ops(partListApiDTO).pageGetAll();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在线编辑 (opens new window)