0

The goal is to send a HTTP GET request containing a list of string representing the enum values QuestionSubject, then use those parameters to select the questions of the right subject. I also added a custom converter to convert the received string to my enum. My problem is that "subjects" is always null when I debug inside the method.

This is my current REST endpoint:

@ResponseBody
@GetMapping(path = "question/getquestionsbysubjects")
public List<Question> loadQuestionsBySubjects(@RequestParam(required=false) QuestionSubject[] subjects) throws IOException, GeneralSecurityException {
   if(subjects == null || subjects.length == 0){
       return this.loadAllQuestions();
   }
   return questionRepository.findByQuestionSubjectIn(Arrays.asList(subjects));
}

I am able to get my questions when passing a single subject in a method with the following signature:

public List<Question> loadQuestionsBySubjects(@RequestParam(required=false) QuestionSubject subject)

So it does not appear to be an issue of converting the string to enum.

I tried sending multiple requests, but subjects is always null in the endpoint. Here's what I already tried using postman:

http://localhost:8080/question/getquestionsbysubjects?subjects=contacts,ko
http://localhost:8080/question/getquestionsbysubjects?subjects=["contacts", "ko"]
http://localhost:8080/question/getquestionsbysubjects?subjects=contacts&subjects=ko

Is there an issue I'm not aware of? Those seems to be working in what I found in other questions.

Here's the converter:

public class StringToQuestionSubjectConverter implements Converter<String, QuestionSubject> {
   @Override
   public QuestionSubject convert(String source) {
     return QuestionSubject.valueOf(source.toUpperCase());
   }

   public Iterable<QuestionSubject> convertAll(String[] sources) {
     ArrayList<QuestionSubject> list = new ArrayList<>();
        for (String source : sources) {
            list.add(this.convert(source));
        }
     return list;
    }
}
adarsh
  • 1,393
  • 1
  • 8
  • 16
Lymn
  • 65
  • 10

1 Answers1

2

Use a List directly:

@RequestParam(required=false) List<QuestionSubject> subjects

return questionRepository.findByQuestionSubjectIn(subjects);
codebrane
  • 4,290
  • 2
  • 18
  • 27