简体   繁体   中英

How to specify request body in Spring boot for swagger UI?

I have the following annotated controller for swagger:

  @PostMapping("/getMediaDataProduct/V2")
    @ResponseBody
    @ApiOperation(value = "get media data product v2")
    @ApiResponses({@ApiResponse(code = 200, message = "Successful", response = MediaDataProductResponseV2.class)})

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "xxxxx", value = "xxxx", paramType = "header", required = true),
            @ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header", required = true),
            @ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header"),
            @ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header", required = true),
            //  @ApiImplicitParam(name = "xxxxxx", value = "xxx", paramType = "header" ,required = true),
            @ApiImplicitParam(name = "xxxxxx", value = "xxxxx", paramType = "header"),
            @ApiImplicitParam(name = "xxxxxxx", value = "xxxxxxxx", paramType = "header", required = true)})

    public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
        Slogger.get().debug("/getMediaDataProduct/V2: this.mediaDataService: " + this.mediaDataService);

        MediaDataProductResponseV2 response = mediaDataService.getMediaDataProductV2(request);
        HttpStatus status = getHttpStatus(response.getStatusMessages(), response.getSystemErrors());
        List<StatusMessage> statusMessages = appendSuccessStatusMessage(response.getStatusMessages(), status);
        if(statusMessages !=null) {
            response.setStatusMessages(statusMessages);
        }
        return new ResponseEntity<>(response, new HttpHeaders(), status);
    }

How can you specify the request body so it's displayed in the swagger UI?

update: I have tried to research on this in many forums but doesn't seem to find an answer. "How can you specify the request body class by using swagger annotations just as I do with ApiResponses?". There is a default parameter in swagger UI created for body but don't know how to specify it by referencing the request body class.

If I understood your problem correctly, you need to have a response type shown in the swagger definition. Most of the time Swagger will automatically map your method return type as response type.

But, it fails to do so when it's a generic class, as in your case its ResponseEntity . If you can make your return type to something like ResponseEntity<MediaDataProductResponseV2> , it should work.

According to Swagger documentation , you can declare paramType attribute at @ApiImplicitParam only with the following values:

Valid values are path, query, body, header or form.

the Default is ""

In your case, try to do something like this.

@ApiImplicitParams(value = {
        @ApiImplicitParam(name = "request", value = "The body is a simple string", paramType = "body", required = true),
        @ApiImplicitParam(name = "headers", value = "It'll contain all header attributes in request", paramType = "header", required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
    Slogger.get().debug("/getMediaDataProduct/V2: this.mediaDataService: " + this.mediaDataService);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM