saran3h
saran3h

Reputation: 14022

Configure Swagger UI to accept a map in Spring boot microservice

I've created an api that takes in a map of values (filters, sorting, pagination etc,) from UI as below:

/**
     * 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);
    }

Here I'm capturing all the information from UI in a map:

@RequestParam MultiValueMap<String, String> allRequestParams

This is the actual request from UI:

https://<base-url>/api/contract/restructure-event/audit?cb=1622617155911&page=0&size=20&fromDate=2021-06-02T23:00:00.000Z

But when I look into the swagger docs, it looks like this: enter image description here

which makes it impossible to test.

Is there any way that I can provide the map values via Swagger UI in a human readable way?

Upvotes: 0

Views: 1625

Answers (1)

zypherscript
zypherscript

Reputation: 246

ApiImplicitParam allows you to manually define a parameter in a fine-tuned manner.

Ex: I have defined two example parameters from your actual request(page & size)...

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

Upvotes: 1

Related Questions