'I need to do dynamic filtering of a get request

I need to do dynamic filtering of a get request.

@GetMapping
public List<Employee> getEmployee(@RequestParam(value = "employeeId") String employeeId, 
                                  @RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "age",required = false) Long age,
                                  @RequestParam(value = "city",required = false) String city,

The request can be like: localhost/employee?employeeId=134&name=Andrey &age=22&city=LA so and localhost/employee-detail?employeeId=134&&city=LA I have never solved such problems and I assume that this can be done using jpa criteria. I tried to do it like this but it didn't work


      EntityManager em = emf.createEntityManager();
      em.getTransaction().begin();
      CriteriaBuilder cb = em.getCriteriaBuilder();

      CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);

      Root<Employee> stud = cq.from(Employee.class);

      cq.multiselect(stud.get("employeeId"),
              stud.get("name"),
              stud.get("age"),
              stud.get("city"),

      CriteriaQuery<Employee> select = cq.select(stud);
      TypedQuery<Employee> q = em.createQuery(select);
      List<Employee> list = q.getResultList();

Help me please



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source