java-Swagger参数文档

当前状态:

我的控制器中有两种方法可以根据传递的参数获取数据.编码:

@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;
    }
}

一切工作正常,但是草率的文档不是我期望的.

java-Swagger参数文档

java-Swagger参数文档

有没有办法从请求网址中删除{?size,cost}?

这是我的证件信息:

@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
    }

解决方法:

Swagger规范不支持基于查询字符串的同一路径的多个文档,因此swagger-ui也不支持.

通过设置enableUrlTemplating(true)启用的功能似乎是springfox中的一个孵化功能,但目前无法与swagger-ui一起使用.

相关讨论可以在这里找到:

> https://github.com/swagger-api/swagger-ui/issues/1665
> https://github.com/springfox/springfox/issues/866

现在看来,您要么不得不使用在swagger-ui中看起来很奇怪的路径,要么必须合并文档.

上一篇:Springboot2.x整合Swagger3.x


下一篇:2021-10-10