Class RestMatcher

java.lang.Object
org.apache.juneau.rest.matcher.RestMatcher
Direct Known Subclasses:
ClientVersionMatcher, MultipartFormDataMatcher, UrlEncodedFormMatcher

public abstract class RestMatcher extends Object
Class used for defining method-level matchers using the @RestOp(matchers) annotation.

Matchers are used to allow multiple Java methods to handle requests assigned to the same URL path pattern, but differing based on some request attribute, such as a specific header value. For example, matchers can be used to provide two different methods for handling requests from two different client versions.

Java methods with matchers associated with them are always attempted before Java methods without matchers. This allows a 'default' method to be defined to handle requests where no matchers match.

When multiple matchers are specified on a method, only one matcher is required to match. This is opposite from the @RestOp(guards) annotation, where all guards are required to match in order to execute the method.

Example:

public class MyResource extends BasicRestServlet { @RestGet(path="/foo", matchers=IsDNT.class) public Object doGetWithDNT() { // Handle request with Do-Not-Track specified } @RestGet("/foo") public Object doGetWithoutDNT() { // Handle request without Do-Not-Track specified } } public class IsDNT extends RestMatcher { @Override public boolean matches(HttpServletRequest req) { return "1".equals(req.getHeader("DNT")); } }

Instances must provide one of the following public constructors:

  • No-args.
  • The following args: Object resource, Method javaMethod.
    This gives access to the servlet/resource and Java method it's applied to.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract boolean
    matches(jakarta.servlet.http.HttpServletRequest req)
    Returns true if the specified request matches this matcher.
    boolean
    Returns true if this matcher is required to match in order for the method to be invoked.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • matches

      public abstract boolean matches(jakarta.servlet.http.HttpServletRequest req)
      Returns true if the specified request matches this matcher.
      Parameters:
      req - The servlet request.
      Returns:
      true if the specified request matches this matcher.
    • required

      public boolean required()
      Returns true if this matcher is required to match in order for the method to be invoked.

      If false, then only one of the matchers must match.

      Returns:
      true if this matcher is required to match in order for the method to be invoked.