简体   繁体   中英

Swagger Param Documentation

Current State:

I have two methods in my controller to get data based on what parameters are passed. The code:

@RestController
@RequestMapping("/samples")
public class SampleController {

    @RequestMapping(value = "/{id}", params = {"cost"}, method = RequestMethod.GET)
    public String getSamplesByIdAndCost(@PathVariable String id, @RequestParam(value = "cost") String cost) {
        return "result";
    }

    @RequestMapping(value = "/{id}", params = {"cost", "size"}, method = RequestMethod.GET)
    public String getSamplesByIdCostAndSize(@PathVariable String id, @RequestParam(value = "cost") String cost,
                                        @RequestParam(value = "size") String size) {
    return "ID : " + id + " / COST : " + cost + " / SIZE : " + size;
    }
}

Everything works fine, but the swagger documentation is not what I expected.

在此输入图像描述

在此输入图像描述

Question

Is there a way to remove {?size,cost} from the Request URL?

Here is my Docket info:

@Bean
    public Docket myApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .directModelSubstitute(LocalDate.class,
                        String.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .alternateTypeRules(
                        newRule(typeResolver.resolve(DeferredResult.class,
                                typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                                typeResolver.resolve(WildcardType.class)))
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET,
                        newArrayList(new ResponseMessageBuilder()
                                .code(500)
                                .message("500 message")
                                .responseModel(new ModelRef("Error"))
                                .build()))
                .enableUrlTemplating(true);

    }

    @Autowired
    TypeResolver typeResolver;

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(
                "validatorUrl",// url
                "none",       // docExpansion          => none | list
                "alpha",      // apiSorter             => alpha
                "schema",     // defaultModelRendering => schema
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS,
                false,        // enableJsonEditor      => true | false
                true);        // showRequestHeaders    => true | false
    }

Multiple documentations for the same path based on query strings is not something that is supported by Swagger spec and therefore not by swagger-ui.

What you have enabled by setting enableUrlTemplating(true) appears to be an incubation feature in springfox but won't work as of now with swagger-ui.

Relevant discussion can be found here:

For now it seem like you either have to live with paths looking weird in the swagger-ui, or you have to merge your documentation.

See Tobias Raski's answer to understand more about why this problem exists.

There is a work around for this. You can see some details here: https://github.com/springfox/springfox/issues/1484

In summary there is an experimental UI that fixes the issue. This may eventually be irrelevant when a future fix comes out.

According to this thread :

Swagger spec does not allow multiple endpoints that differ by request params. However you could use the experimental feature to get around it using docket#enableUrlTemplates(true) . Keep in mind it is experimental. Also please upgrade to 2.7.0

删除@RequestMapping注释中的'params'属性,您的代码仍然有效。

you can change maven pom

<dependency>
    <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
 <dependency>
    <groupId>io.springfox.ui</groupId>
        <artifactId>springfox-swagger-ui-rfc6570</artifactId>
        <version>1.0.0</version>
</dependency>

use springfox-swagger-ui-rfc6570 instead of springfox-swagger-ui

Click here to see more.

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