我使用swagger来显示我的RESTful API,API的一个参数将字符串作为输入并将其转换为枚举值。有没有办法显示获取Swagger UI的键是下拉菜单而不是文本输入。
答案 0 :(得分:1)
I am posting full example here.swagger 2 configuration you can do from here<link>http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api</link>
Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
QuestionCategory.java
import java.util.Arrays;
public enum QuestionCategory {
CSE("cse"),ECE("ece");
private String value;
private QuestionCategory(String value) {
this.value = value;
}
public static QuestionCategory fromValue(String value) {
for (QuestionCategory category : values()) {
if (category.value.equalsIgnoreCase(value)) {
return category;
}
}
throw new IllegalArgumentException(
"Unknown enum type " + value + ", Allowed values are " + Arrays.toString(values()));
}
}
Question.java
public class Question {
private QuestionCategory type;
private String question;
private String answer;
public QuestionCategory getType() {
return type;
}
public void setType(QuestionCategory type) {
this.type = type;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
QuestionController.java
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.devglan.model.Question;
import com.devglan.model.QuestionCategory;
import com.devglan.model.QuestionCategoryConverter;
@RestController
public class QuestionController {
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
public List get(@PathVariable(value = "type") QuestionCategory category) {
return getQuestionsByCategory(category);
}
private List getQuestionsByCategory(QuestionCategory category) {
List questions = new ArrayList();
Question question = new Question();
question.setType(category);
if(category == QuestionCategory.CSE){
question.setQuestion("What is Operating System.");
question.setAnswer("This is the answer of what is os.");
} else if(category == QuestionCategory.ECE){
question.setQuestion("What is a transistor.");
question.setAnswer("This is the answer of what is transistor.");
}
questions.add(question);
return questions;
}
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(QuestionCategory.class, new QuestionCategoryConverter());
}
}
import java.beans.PropertyEditorSupport;
public class QuestionCategoryConverter extends PropertyEditorSupport{
public void setAsText(final String text) throws IllegalArgumentException {
setValue(QuestionCategory.fromValue(text));
}
}