Open API Guidelines: Implementing Multiple Parameter Filtering APIs

I am currently implementing a Rest API for a search query. The method accepts several filter parameters, such as keyword, name, title, description, date, etc. To stay dry and avoid repetition in the case of else, I am looking for a best practice way of retrieving a resource from a database. Let's say the database provides two methods: findByUsername(...)and findByDescription(), and we want to expose them through a calm web interface. A naive implementation will use the if and else option, similar to the one below.

@GET
@Path("users")
public User getUsers(@QueryParam("username") String username, 
    @QueryParam("description") String description) {
    User user = null;
    if (username != null) {
        // execute findByUsername method to the database
        user = findByUsername(username);
    } else if (description != null) {
        // execute findByDescription method to the database
        user = findByDescription(description);
    }
    return user;
}

The question is how to improve the code above to avoid redundant if else (check) the article and continue to be DRY? I am implementing web services using jQuery jQuery 2.0

+4
1

, .

. , MongoDB .

, { : "X" }, , . {: "Y" }, , . , { : "X", : "Y" }, , .

POST REST, . XML JSON. JSON, MongoDB ( ).

, , , . , .

+3

All Articles