[英]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.