How to get Swagger UI's Parameter to be Dropdown menu instead of Text Input
Solution 1
The key is to use allowableValues
in the @ApiParam
annotation.
The demo showing the result:
http://petstore.swagger.io/#!/pet/findPetsByStatus
Check out pet/findByStatus
, it's not a dropdown but input is limited in
the multi-select box.
Solution 2
you can display dropdown using following code of swagger. You have to use enum. e.g. if you want to take gender as input then there can be three possible values
- male, female, other
-name: "gender"
in: "query"
type: "string"
enum: [ "male", "female", "other"]
description: "Enter user gender here."
required: true
Solution 3
You can directly use enum instead of String parameter as a API parameter.
@RequestMapping(value = "/test", method = RequestMethod.POST)
public void test(EnumTest enum) {
// body
}
EnumTest.java
public enum EnumTest {
One("One"),
Two("Two");
private String str;
EnumTest(String str){
this.str = str;
}
public String getStr() {
return str;
}
}
Related videos on Youtube
mirage1s7
Updated on July 05, 2022Comments
-
mirage1s7 almost 2 years
I am using swagger to display my RESTApi, one parameter of an API takes string as input and convert it to enum value. Is there any way to display a drop-down menu on the Swagger UI instead of having a text input field so that users can only select the string values within the enum value.
-
aradil over 8 yearsSadly those links are now dead.
-
Vinod Kumar Marupu almost 7 yearsHow to get Swagger UI's key to be Dropdown menu instead of Text Input
-
saran3h about 2 yearsunfortunately there is no such alternative in openapi.
-
David S about 2 yearsDoes not produce a drop-down menu in the swagger UI...