Class JsonPathResultMatchers

java.lang.Object
org.springframework.test.web.servlet.result.JsonPathResultMatchers

public class JsonPathResultMatchers extends Object
Factory for assertions on the response content using JsonPath expressions.

An instance of this class is typically accessed via MockMvcResultMatchers.jsonPath(String, Object...).

Since:
3.2
Author:
Rossen Stoyanchev, Craig Andrews, Sam Brannen, Brian Clozel
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    JsonPathResultMatchers(String expression, Object... args)
    Protected constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Evaluate the JSON path expression against the response content and assert that a non-null value does not exist at the given path.
    Evaluate the JSON path expression against the supplied content and assert that a value, including null values, does not exist at the given path.
    Evaluate the JSON path expression against the response content and assert that a non-null value, possibly an empty array or map, exists at the given path.
    Evaluate the JSON path expression against the response content and assert that a value, possibly null, exists.
    Evaluate the JSON path expression against the response content and assert that the result is an array.
    Evaluate the JSON path expression against the response content and assert that the result is a Boolean.
    Evaluate the JSON path expression against the response content and assert that an empty value exists at the given path.
    Evaluate the JSON path expression against the response content and assert that the result is a Map.
    Evaluate the JSON path expression against the response content and assert that a non-empty value exists at the given path.
    Evaluate the JSON path expression against the response content and assert that the result is a Number.
    Evaluate the JSON path expression against the response content and assert that the result is a String.
    prefix(String prefix)
    Configures the current JsonPathResultMatchers instance to verify that the JSON payload is prepended with the given prefix.
    value(@Nullable Object expectedValue)
    Evaluate the JSON path expression against the response content and assert that the result is equal to the supplied value.
    value(Matcher<? super T> matcher)
    Evaluate the JSON path expression against the response content and assert the resulting value with the given Hamcrest Matcher.
    value(Matcher<? super T> matcher, Class<T> targetType)
    An overloaded variant of value(Matcher) that also accepts a target type for the resulting value that the matcher can work reliably against.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • prefix

      public JsonPathResultMatchers prefix(String prefix)
      Configures the current JsonPathResultMatchers instance to verify that the JSON payload is prepended with the given prefix.

      Use this method if the JSON payloads are prefixed to avoid Cross Site Script Inclusion (XSSI) attacks.

      Parameters:
      prefix - the string prefix prepended to the actual JSON payload
      Since:
      4.3
    • value

      public <T> ResultMatcher value(Matcher<? super T> matcher)
      Evaluate the JSON path expression against the response content and assert the resulting value with the given Hamcrest Matcher.
      See Also:
    • value

      public <T> ResultMatcher value(Matcher<? super T> matcher, Class<T> targetType)
      An overloaded variant of value(Matcher) that also accepts a target type for the resulting value that the matcher can work reliably against.

      This can be useful for matching numbers reliably — for example, to coerce an integer into a double.

      Since:
      4.3.15
      See Also:
    • value

      public ResultMatcher value(@Nullable Object expectedValue)
      Evaluate the JSON path expression against the response content and assert that the result is equal to the supplied value.
      See Also:
    • exists

      public ResultMatcher exists()
      Evaluate the JSON path expression against the response content and assert that a non-null value, possibly an empty array or map, exists at the given path.

      If the JSON path expression is not definite, this method asserts that the value at the given path is not empty.

    • doesNotExist

      public ResultMatcher doesNotExist()
      Evaluate the JSON path expression against the response content and assert that a non-null value does not exist at the given path.

      If the JSON path expression is not definite, this method asserts that the value at the given path is empty.

    • isEmpty

      public ResultMatcher isEmpty()
      Evaluate the JSON path expression against the response content and assert that an empty value exists at the given path.

      For the semantics of empty, consult the Javadoc for ObjectUtils.isEmpty(Object).

      Since:
      4.2.1
      See Also:
    • isNotEmpty

      public ResultMatcher isNotEmpty()
      Evaluate the JSON path expression against the response content and assert that a non-empty value exists at the given path.

      For the semantics of empty, consult the Javadoc for ObjectUtils.isEmpty(Object).

      Since:
      4.2.1
      See Also:
    • hasJsonPath

      public ResultMatcher hasJsonPath()
      Evaluate the JSON path expression against the response content and assert that a value, possibly null, exists.

      If the JSON path expression is not definite, this method asserts that the list of values at the given path is not empty.

      Since:
      5.0.3
      See Also:
    • doesNotHaveJsonPath

      public ResultMatcher doesNotHaveJsonPath()
      Evaluate the JSON path expression against the supplied content and assert that a value, including null values, does not exist at the given path.

      If the JSON path expression is not definite, this method asserts that the list of values at the given path is empty.

      Since:
      5.0.3
      See Also:
    • isString

      public ResultMatcher isString()
      Evaluate the JSON path expression against the response content and assert that the result is a String.
      Since:
      4.2.1
    • isBoolean

      public ResultMatcher isBoolean()
      Evaluate the JSON path expression against the response content and assert that the result is a Boolean.
      Since:
      4.2.1
    • isNumber

      public ResultMatcher isNumber()
      Evaluate the JSON path expression against the response content and assert that the result is a Number.
      Since:
      4.2.1
    • isArray

      public ResultMatcher isArray()
      Evaluate the JSON path expression against the response content and assert that the result is an array.
    • isMap

      public ResultMatcher isMap()
      Evaluate the JSON path expression against the response content and assert that the result is a Map.
      Since:
      4.2.1