Matchers
RestMatchers are used to allow multiple Java methods to be tied to the same HTTP method and path but differentiated by some request attribute such as a specific header value.
Example
// GET method that gets invoked for administrators
@RestGet(path="/*", matchers=IsAdminMatcher.class)
public Object doGetForAdmin() {
...
}
// GET method that gets invoked for everyone else
@RestGet("/*")
public Object doGetForEveryoneElse() {
...
}
The interface for matchers is simple:
public class IsAdminMatcher extends RestMatcher {
@Override /* RestMatcher */
public boolean matches(RestRequest req) {
return req.isUserInRole("ADMINS_GROUP");
}
}
note
- If no methods are found with a matching matcher, a 412 Precondition Failed status is returned.
- If multiple matchers are specified on the same method, ONLY ONE matcher needs to match for the method to be invoked.
- Note that you CANNOT define identical paths on different methods UNLESS you use matchers.
That includes paths that are only different in variable names (e.g.
/foo/{bar}
and/foo/{baz}
). If you try to do so, a ServletException will be thrown on startup. - Methods with matchers take precedence over methods without. Otherwise, methods are attempted in the order they appear in the class.
The following matchers are provided: