0

I would like to write a REST api to do a IN query as follows:

 @GetMapping("/student")
  public ResponseEntity<List<Student>> getStudents(
      @RequestParam String searchQuery) {

    return ResponseEntity.ok().body(studentService.searchStudents(searchQuery));
  }

Associated service implementation is as follows:

  public List<Student> searchStudents(String queryParam) {
    String baseQuery = "SELECT * from student ";
    if(!queryParam.isBlank()) {
      /*I cannot understand what should I write here to form the final query as follows:

            SELECT *from student where id in (id1, id2,..);

         Here id1, id2.. should from query param */
          
    }
    ...
  }

As I mentioned above, I cannot figure out how will I pass the query param in the REST API and parse it downstream to form the final IN query for the underlying DB table.

Joy
  • 4,197
  • 14
  • 61
  • 131
  • What do you expect value of `searchQuery` to be? --- A comma-separated list of id's? Space-separated? Colon-separated? What? --- Are the id's integers? UUID? Strings? What? – Andreas Aug 10 '20 at 04:43
  • first of all, passing SQL through REST is strictly not recommended. Second: do you use any frameworks as Hibernate? if not, use jdbc library, open connection, execute string – Kampaii Aug 10 '20 at 04:47

0 Answers0