java - 在 Spring Boot 微服务中配置 Swagger UI 以接受映射
问题描述
我创建了一个 api,它从 UI 中获取值映射(过滤器、排序、分页等),如下所示:
/**
* Retrieves audit records for events
*
* @param allRequestParams filters
* @return search results with list of {@link Event}
*/
@ApiOperation(value = "Retrieve audit records for events")
@GetMapping("/audit")
public ResponseEntity getAuditRecords(
@RequestParam MultiValueMap<String, String> allRequestParams
) throws PermissionsDeniedException, GenericException, GeneralSecurityException {
log.info("Retrieving audit records of all events...");
List<Event> events = eventService.getAuditRecords(userPrincipleService.getUserPrinciple(), allRequestParams);
if (isEmpty(events)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(getPaginatedResponse(allRequestParams, events), HttpStatus.OK);
}
在这里,我在地图中从 UI 中捕获所有信息:
@RequestParam MultiValueMap<String, String> allRequestParams
这是来自 UI 的实际请求:
https://<base-url>/api/contract/restructure-event/audit?cb=1622617155911&page=0&size=20&fromDate=2021-06-02T23:00:00.000Z
这使得无法测试。
有什么方法可以通过 Swagger UI 以人类可读的方式提供地图值?
解决方案
ApiImplicitParam允许您以微调的方式手动定义参数。
例如:我从您的实际请求中定义了两个示例参数(页面和大小)......
@ApiOperation(value = "Retrieve audit records for events")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "enter page value.", dataTypeClass = Integer.class, paramType = "query")
,@ApiImplicitParam(name = "size", value = "enter size value.", dataTypeClass = Integer.class, paramType = "query")
})
@GetMapping("/audit")
public ResponseEntity getAuditRecords(
@RequestParam MultiValueMap<String, String> allRequestParams
) throws PermissionsDeniedException, GenericException, GeneralSecurityException {
//...
}
推荐阅读
- c# - 使用 C# asp.net 的 Google My Business Insights api
- ios - 在 ResearchKit 中进行调查之前需要征得同意
- jquery - JQuery 可嵌套和可排序限制
- javascript - 无法在 ui:composition 和 ui:define 中将 div 转换为 pdf
- python - 如何将交互过程的返回值从 docker 复制到 WebSocket?
- node.js - 如何使用 Nodejs AWS Lambda 应用程序的“大”测试数据处理单元测试
- json - Flask、json、render_template、表单动作、数据表
- python - python - 如何解决“ValueError:传递值的形状为 (228, 1),索引暗示 (228, 7)”
- javascript - jQuery click 事件的 event.target 返回被点击元素的子元素
- java - 将本地项目作为依赖项导入时无法使用 maven 编译