【问题标题】:How to get Swagger UI's key to be Dropdown menu instead of Text Input如何让 Swagger UI 的键成为下拉菜单而不是文本输入
【发布时间】:2017-08-25 12:58:28
【问题描述】:

我正在使用 swagger 来显示我的 RESTful API,API 的一个参数将字符串作为输入并将其转换为枚举值。有什么方法可以显示获取 Swagger UI 的键是下拉菜单而不是文本输入。

【问题讨论】:

  • @spandey15 我正在研究 Nodejs。请张贴招摇文件。
  • @Vinod Kumar Marupu 抱歉不知道节点 js。

标签: rest swagger swagger-2.0


【解决方案1】:

我在此处发布完整示例。您可以从此处http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api 进行 swagger 2 配置

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()));
    }

}

问题.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());
    }
}

QuestionCategoryConverter.java

import java.beans.PropertyEditorSupport;

public class QuestionCategoryConverter extends PropertyEditorSupport{

    public void setAsText(final String text) throws IllegalArgumentException {
        setValue(QuestionCategory.fromValue(text));
    }
}

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2011-05-30
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多