Class RequestBodyExtractionHelper

java.lang.Object
org.mockserver.templates.engine.helpers.RequestBodyExtractionHelper

public class RequestBodyExtractionHelper extends Object
Shared JSONPath / XPath extraction helper used by the Velocity and JavaScript template engines so those engines extract request-body fields with the exact same underlying libraries, error handling and return semantics as the Mustache engine's jsonPath / xPath lambdas.

Both methods operate on the current request body (via HttpRequest.getBodyAsJsonOrXmlString()). On a missing path or any evaluation error they mirror Mustache: the error is logged and an empty value is returned (empty string for xPath, empty string for jsonPath) rather than throwing, so a template that references a non-existent path still renders.

Author:
jamesdbloom
  • Constructor Details

  • Method Details

    • jsonPath

      public Object jsonPath(String jsonPath)
      Evaluate a JSONPath expression against the request body. Returns the extracted value (which may be a scalar, list or map depending on the expression), or an empty string if the path is missing, resolves to a JSON null, or evaluation fails. (Templates render null as empty anyway, so coercing to "" keeps Velocity/JavaScript output consistent with Mustache.)
    • xPath

      public String xPath(String xPath)
      Evaluate an XPath expression against the request body, returning the result as a string (the same XPathConstants.STRING return type the Mustache engine uses), or an empty string if the path is missing or evaluation fails.
    • toString

      public String toString()
      Overrides:
      toString in class Object