[英]Swagger Param Documentation
当前状态:
我的控制器中有两种方法可以根据传递的参数获取数据。 代码:
@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;
}
}
一切正常,但招摇文件不是我的预期。
题
有没有办法从请求网址中删除{?size,cost}?
这是我的Docket信息:
@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。
相关讨论可以在这里找到:
现在看起来你要么必须忍受在swagger-ui中看起来很怪异的路径,要么你必须合并你的文档。
请参阅Tobias Raski的答案,以了解更多有关此问题存在的原因。
有一个解决方案。 你可以在这里看到一些细节: https : //github.com/springfox/springfox/issues/1484
总之,有一个实验用户界面可以解决问题。 当未来的解决方案出来时,这可能最终无关紧要。
根据这个帖子 :
Swagger规范不允许多个端点因请求参数而异。 但是你可以使用实验性功能来解决它使用
docket#enableUrlTemplates(true)
。 请记住它是实验性的。 另请升级到2.7.0
删除@RequestMapping注释中的'params'属性,您的代码仍然有效。
你可以改变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>
使用springfox-swagger-ui-rfc6570
而不是springfox-swagger-ui
点击这里查看更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.