Class RequestQueryParams

All Implemented Interfaces:
Serializable, Cloneable, Iterable<RequestQueryParam>, Collection<RequestQueryParam>, List<RequestQueryParam>, RandomAccess

Represents the query parameters in an HTTP request.

The RequestQueryParams object is the API for accessing the GET query parameters of an HTTP request. It can be accessed by passing it as a parameter on your REST Java method:

@RestPost(...) public Object myMethod(RequestQueryParams query) {...}

Example:

@RestPost(...) public Object myMethod(RequestQueryParams query) { // Get query parameters converted to various types. int p1 = query.get("p1").asInteger().orElse(0); String p2 = query.get("p2").orElse(null); UUID p3 = query.get("p3").as(UUID.class).orElse(null); }

An important distinction between the behavior of this object and HttpServletRequest.getParameter(String) is that the former will NOT load the content of the request on FORM POSTS and will only look at parameters found in the query string. This can be useful in cases where you're mixing GET parameters and FORM POSTS and you don't want to inadvertently read the content of the request to get a query parameter.

Some important methods on this class are:

Entries are stored in a case-sensitive map unless overridden via the constructor.

See Also:
See Also:
  • Constructor Details

    • RequestQueryParams

      public RequestQueryParams(RestRequest req, Map<String,String[]> query, boolean caseSensitive)
      Constructor.
      Parameters:
      req - The request creating this bean.
      query - The raw parsed query parameter values.
      caseSensitive - Whether case-sensitive name matching is enabled.
  • Method Details

    • parser

      Sets the parser to use for part values.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • caseSensitive

      public RequestQueryParams caseSensitive(boolean value)
      Sets case sensitivity for names in this list.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object (for method chaining).
    • addDefault

      public RequestQueryParams addDefault(List<? extends NameValuePair> pairs)
      Adds default entries to these parameters.

      Similar to set(String, Object) but doesn't override existing values.

      Parameters:
      pairs - The default entries.
      Can be null.
      Returns:
      This object.
    • addDefault

      Adds default entries to these parameters.

      Similar to set(String, Object) but doesn't override existing values.

      Parameters:
      pairs - The default entries.
      Can be null.
      Returns:
      This object.
    • addDefault

      public RequestQueryParams addDefault(String name, String value)
      Adds a default entry to the query parameters.
      Parameters:
      name - The name.
      value - The value.
      Returns:
      This object.
    • add

      public RequestQueryParams add(String name, Object value)
      Adds a parameter value.

      Parameter is added to the end.
      Existing parameter with the same name are not changed.

      Parameters:
      name - The parameter name. Must not be null.
      value - The parameter value.
      Returns:
      This object.
    • add

      public RequestQueryParams add(NameValuePair... parameters)
      Adds request parameter values.

      Parameters are added to the end.
      Existing parameters with the same name are not changed.

      Parameters:
      parameters - The parameter objects. Must not be null.
      Returns:
      This object.
    • set

      public RequestQueryParams set(String name, Object value)
      Sets a parameter value.

      Parameter is added to the end.
      Any previous parameters with the same name are removed.

      Parameters:
      name - The parameter name. Must not be null.
      value - The parameter value.
      Converted to a string using Object.toString().
      Can be null.
      Returns:
      This object.
    • set

      public RequestQueryParams set(NameValuePair... parameters)
      Sets request header values.

      Parameters are added to the end of the headers.
      Any previous parameters with the same name are removed.

      Parameters:
      parameters - The parameters to set. Must not be null or contain null.
      Returns:
      This object.
    • remove

      Remove parameters.
      Parameters:
      name - The parameter names. Must not be null.
      Returns:
      This object.
    • subset

      public RequestQueryParams subset(String... names)
      Returns a copy of this object but only with the specified param names copied.
      Parameters:
      names - The list to include in the copy.
      Returns:
      A new list object.
    • contains

      public boolean contains(String name)
      Returns true if the parameters with the specified name is present.
      Parameters:
      name - The parameter name. Must not be null.
      Returns:
      true if the parameters with the specified names are present.
    • containsAny

      public boolean containsAny(String... names)
      Returns true if the parameter with any of the specified names are present.
      Parameters:
      names - The parameter names. Must not be null.
      Returns:
      true if the parameter with any of the specified names are present.
    • getAll

      Returns all the parameters with the specified name.
      Parameters:
      name - The parameter name.
      Returns:
      The list of all parameters with the specified name, or an empty list if none are found.
    • stream

      Returns all headers with the specified name.
      Parameters:
      name - The header name.
      Returns:
      The stream of all headers with matching names. Never null.
    • getSorted

      Returns all headers in sorted order.
      Returns:
      The stream of all headers in sorted order.
    • getNames

      public List<String> getNames()
      Returns all the unique header names in this list.
      Returns:
      The list of all unique header names in this list.
    • getFirst

      Returns the first parameter with the specified name.

      Note that this method never returns null and that RequestHttpPart.isPresent() can be used to test for the existence of the parameter.

      Parameters:
      name - The parameter name.
      Returns:
      The parameter. Never null.
    • getLast

      Returns the last parameter with the specified name.

      Note that this method never returns null and that RequestHttpPart.isPresent() can be used to test for the existence of the parameter.

      Parameters:
      name - The parameter name.
      Returns:
      The parameter. Never null.
    • get

      public RequestQueryParam get(String name)
      Returns the condensed header with the specified name.

      If multiple headers are present, they will be combined into a single comma-delimited list.

      Parameters:
      name - The header name.
      Returns:
      The header, never null.
    • get

      public <T> Optional<T> get(Class<T> type)
      Returns the query parameter as the specified bean type.

      Type must have a name specified via the Query annotation and a public constructor that takes in either value or name,value as strings.

      Type Parameters:
      T - The bean type to create.
      Parameters:
      type - The bean type to create.
      Returns:
      The bean, never null.
    • asQueryString

      Converts this object to a query string.

      Returned query string does not start with '?'.

      Returns:
      A new query string, or an empty string if this object is empty.
    • copy

      Makes a copy of these parameters.
      Returns:
      A new parameters object.
    • getSearchArgs

      Locates the search query argument (&amp;s=) in the query string and returns them as a SearchArgs object.
      Returns:
      A new SearchArgs object initialized with the query arguments, or Optional.empty() if not found.
    • getViewArgs

      Locates the view query argument (&amp;v=) in the query string and returns them as a ViewArgs object.
      Returns:
      A new ViewArgs object initialized with the query arguments, or Optional.empty() if not found.
    • getSortArgs

      Locates the sort query argument (&amp;o=) in the query string and returns them as a SortArgs object.
      Returns:
      A new SortArgs object initialized with the query arguments, or Optional.empty() if not found.
    • getPageArgs

      Locates the position/limit query arguments (&amp;p=, &amp;l=) in the query string and returns them as a PageArgs object.
      Returns:
      A new PageArgs object initialized with the query arguments, or Optional.empty() if not found.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<RequestQueryParam>