1

I am building a restful api's using spring mvc and spring jpa for database connection. I have requirement to create a get api which can filter the results based on filterString(passed as query parameter to GET request).

Example GET api for filter employee objects is

http://localhost:8080/api/v1/employee?filter="(firstName eq john) and (lastName eq doe) or (empId eq 123)"

Currently I am achieving this by parse the filterString using regX and create the "spring jpa Specification" object from it

Below is the code snippet

public List<Employee> searchEmployee(String filter) throws Exception {
        // filter = "/"(firstName eq john) and (lastName eq doe) or (empId eq 123)/""
        // remove the " characters from start and end
        filter = filter.replaceAll("^\"|\"$", "");

        // spit the string basis of and/or
        String[] value = filter.split("(((?<=or)|(?=or)))|(((?<=and)|(?=and)))");
        Specification specs = null;
        String op = null;
        for (String f : value) {
            if (!"or".equalsIgnoreCase(f) && !"and".equalsIgnoreCase(f)) {
                String[] p = f.trim().split("\\s{1,}");
                if (p != null && p.length == 3) {
                    EmployeeSpecification es = new EmployeeSpecification(new SearchCriteria(p[0], p[1], p[2]));
                    if (specs == null ) {
                        specs = Specification.where(es);
                    } else {
                        if ("or".equalsIgnoreCase(op)) {
                            specs = specs.or(es);
                        } else if ("or".equalsIgnoreCase(op)) {
                            specs = specs.and(es);
                        }
                    }

                } else {
                    throw new Exception("Invalid search criteria");
                }

            } else {
                op = f;
            }


           List<Employee> l = empDao.findAll(specs);

          return l;

        }

I have seen many REST api's which support the filtering like this. Can anyone suggest what is the best way to implement filtering on RESt server side?

6
  • 1
    While certain frameworks like GraphQL and RestQL are usually recommended for such tasks, they actually just promote the RPC style many APIs have, even though they market their API as RESTful, as these libraries need to know all of the available fields upfront and therefore couple to the type of the resource closely. A server should always be in control of its URIs, especially as they are used as cache keys also. ... Commented Jun 10, 2019 at 19:58
  • ... A client should only use URIs given by the server. You might use a form representation to teach a client on what elements it might filter and provide the client with a template URI it can fill in the encoded values from the form and invoke the request on the server. The media-type of course has to support such a feature. How you design such a URI is completely up to you though. Note further that you probably shouldn't need to version your URIs in a REST architecture Commented Jun 10, 2019 at 20:01
  • xkcd.com/327 Commented Jun 10, 2019 at 21:17
  • @RomanVottner How to create rest service itself (RESTful API in String) for RestQl? Commented Dec 19, 2019 at 15:38
  • @Alex78191 As mentioned already, with GraphQL or RestQL, despite the name implication, you won't achieve REST (in its true sense) with such things, and REST doesn't need such things actually. A server that allows a client to filter certain aspects will provide clients with either forms that teach a client on which fields are available for filtering or provide a set of URIs a client can chose from to retrieve the filtered state it is interested in. Nowhere should a REST client be forced to use any external documentation or DSL to retrieve what it is interested in Commented Dec 19, 2019 at 15:51

1 Answer 1

2

I use rsql-parser in my projects. I parse the query string creating the criteria object or the sql query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.