首页 > 解决方案 > 在 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 文档时,它看起来像这样: 在此处输入图像描述

这使得无法测试。

有什么方法可以通过 Swagger UI 以人类可读的方式提供地图值?

标签: javaspring-bootswagger

解决方案


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 {
        //...
    }

推荐阅读