org.springframework.web.servlet.mvc.condition

## Class HeadersRequestCondition

- java.lang.Object
- - org.springframework.web.servlet.mvc.condition.AbstractRequestCondition<HeadersRequestCondition>
  - - org.springframework.web.servlet.mvc.condition.HeadersRequestCondition

- All Implemented Interfaces:
    RequestCondition<HeadersRequestCondition>

      public final class HeadersRequestCondition
      extends AbstractRequestCondition<HeadersRequestCondition>

  A logical conjunction (' && ') request condition that matches a request against a set of header expressions with syntax defined in `RequestMapping.headers()`.

  Expressions passed to the constructor with header names 'Accept' or 'Content-Type' are ignored. See `ConsumesRequestCondition` and `ProducesRequestCondition` for those.

  Since:
    3.1
  Author:
    Arjen Poutsma, Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `HeadersRequestCondition(String... headers)` Create a new instance from the given header expressions.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `HeadersRequestCondition` | `combine(HeadersRequestCondition other)` Returns a new instance with the union of the header expressions from "this" and the "other" instance.
    `int` | `compareTo(HeadersRequestCondition other, HttpServletRequest request)` Returns: 0 if the two conditions have the same number of header expressions Less than 0 if "this" instance has more header expressions Greater than 0 if the "other" instance has more header expressions
    `protected Collection<org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.HeaderExpression>` | `getContent()` Return the discrete items a request condition is composed of.
    `Set<NameValueExpression<String>>` | `getExpressions()` Return the contained request header expressions.
    `HeadersRequestCondition` | `getMatchingCondition(HttpServletRequest request)` Returns "this" instance if the request matches all expressions; or `null` otherwise.
    `protected String` | `getToStringInfix()` The notation to use when printing discrete items of content.

    - ### Methods inherited from class org.springframework.web.servlet.mvc.condition.AbstractRequestCondition

      `equals, hashCode, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### HeadersRequestCondition

          public HeadersRequestCondition(String... headers)

      Create a new instance from the given header expressions. Expressions with header names 'Accept' or 'Content-Type' are ignored. See `ConsumesRequestCondition` and `ProducesRequestCondition` for those.
      Parameters:
        `headers` - media type expressions with syntax defined in `RequestMapping.headers()`; if 0, the condition will match to every request.

  - ### Method Detail

    - #### getExpressions

          public Set<NameValueExpression<String>> getExpressions()

      Return the contained request header expressions.

    - #### getContent

          protected Collection<org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.HeaderExpression> getContent()

      Description copied from class: `AbstractRequestCondition`
      Return the discrete items a request condition is composed of. For example URL patterns, HTTP request methods, param expressions, etc.
      Specified by:
        `getContent` in class `AbstractRequestCondition<HeadersRequestCondition>`
      Returns:
        a collection of objects, never `null`

    - #### getToStringInfix

          protected String getToStringInfix()

      Description copied from class: `AbstractRequestCondition`
      The notation to use when printing discrete items of content. For example " || " for URL patterns or " && " for param expressions.
      Specified by:
        `getToStringInfix` in class `AbstractRequestCondition<HeadersRequestCondition>`

    - #### combine

          public HeadersRequestCondition combine(HeadersRequestCondition other)

      Returns a new instance with the union of the header expressions from "this" and the "other" instance.
      Parameters:
        `other` - the condition to combine with.
      Returns:
        a request condition instance that is the result of combining the two condition instances.

    - #### getMatchingCondition

          public HeadersRequestCondition getMatchingCondition(HttpServletRequest request)

      Returns "this" instance if the request matches all expressions; or `null` otherwise.
      Returns:
        a condition instance in case of a match; or `null` if there is no match.

    - #### compareTo

          public int compareTo(HeadersRequestCondition other,
                      HttpServletRequest request)

      Returns:
      - 0 if the two conditions have the same number of header expressions
      - Less than 0 if "this" instance has more header expressions
      - Greater than 0 if the "other" instance has more header expressions

      It is assumed that both instances have been obtained via `getMatchingCondition(HttpServletRequest)` and each instance contains the matching header expression only or is otherwise empty.