[英]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? 有没有办法从请求网址中删除{?size,cost}?
Here is my Docket info: 这是我的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
}
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. 基于查询字符串的相同路径的多个文档不是Swagger规范支持的,因此不是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. 您通过设置enableUrlTemplating(true)
启用的功能似乎是springfox中的孵化功能,但现在无法使用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. 现在看起来你要么必须忍受在swagger-ui中看起来很怪异的路径,要么你必须合并你的文档。
See Tobias Raski's answer to understand more about why this problem exists. 请参阅Tobias Raski的答案,以了解更多有关此问题存在的原因。
There is a work around for this. 有一个解决方案。 You can see some details here: https://github.com/springfox/springfox/issues/1484 你可以在这里看到一些细节: 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. Swagger规范不允许多个端点因请求参数而异。 However you could use the experimental feature to get around it using
docket#enableUrlTemplates(true)
. 但是你可以使用实验性功能来解决它使用docket#enableUrlTemplates(true)
。 Keep in mind it is experimental. 请记住它是实验性的。 Also please upgrade to 2.7.0 另请升级到2.7.0
删除@RequestMapping注释中的'params'属性,您的代码仍然有效。
you can change maven pom 你可以改变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
使用springfox-swagger-ui-rfc6570
而不是springfox-swagger-ui
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.