Vinod Kumar Marupu
Vinod Kumar Marupu

Reputation: 547

How to get Swagger UI's key to be Dropdown menu instead of Text Input

I am using swagger to display my RESTful API, one parameter of an API takes string as input and convert it to enum value. Is there any way to display get Swagger UI's key to be Dropdown menu instead of Text Input.

Upvotes: 2

Views: 5064

Answers (1)

ssaa
ssaa

Reputation: 1112

I am posting full example here.swagger 2 configuration you can do from here http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

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

QuestionCategoryConverter.java

import java.beans.PropertyEditorSupport;

public class QuestionCategoryConverter extends PropertyEditorSupport{

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

Upvotes: 1

Related Questions