Package org.mockserver.matchers
Class BodyMatching
java.lang.Object
org.mockserver.matchers.BodyMatching
Shared body-matching dispatch used by BOTH request matching
(
HttpRequestPropertiesMatcher) and response matching (HttpResponseMatcher).
This is the single source of truth for "given a compiled BodyMatcher and an actual
message body, does the body match?". It owns the full matcher dispatch the request matcher
historically had inline — and which the response matcher previously lacked:
- optional-body short-circuit — an optional template body matches a message with no body at all;
- multipart — a
MultipartMatcherdecodes the raw body bytes using theContent-Typeboundary; - binary original/compressed dual-match — a
BinaryMatcheraccepts a match against either the decompressed bytes or the original (e.g. still-compressed) bytes; - JSON / JSON schema / JSON path — the actual body is first run through
JsonSchemaBodyDecoder.convertToJson(BodySource, BodyMatcher)so an XML or form actual body is converted to JSON before matching, and an absent body is tolerated (a clean non-match, never an internal NPE); - XML / XML schema / GraphQL / JSON-RPC / string — matched against the body string.
The dispatch is parameterised over a BodySource (the small set of accessors the dispatch
needs) and an optional MatchDifference context. The request path passes a non-null
context so its diagnostics are unchanged; the response path may pass null (response
diagnostics are a later addition). The request-only control-plane BodyDTO re-parse fallback
is intentionally NOT part of this helper — it stays on the request path.
-
Method Summary
Modifier and TypeMethodDescriptionstatic BodySourceof(HttpRequest request) Adapts anHttpRequestto theBodySourceabstraction, exposing the request's per-request XML→JSON cache (HttpRequest.getOrComputeConvertedBody(org.mockserver.model.HttpRequest.ConvertedBodyType, java.util.function.Supplier<java.lang.String>)) so the conversion is memoised exactly as on the historical request path, and carrying the request itself for the JSON-conversion failure log.static BodySourceof(HttpResponse response) Adapts anHttpResponseto theBodySourceabstraction.
-
Method Details
-
of
Adapts anHttpRequestto theBodySourceabstraction, exposing the request's per-request XML→JSON cache (HttpRequest.getOrComputeConvertedBody(org.mockserver.model.HttpRequest.ConvertedBodyType, java.util.function.Supplier<java.lang.String>)) so the conversion is memoised exactly as on the historical request path, and carrying the request itself for the JSON-conversion failure log. -
of
Adapts anHttpResponseto theBodySourceabstraction. A response carries no compressed-original body and no per-message conversion cache, so#getOriginalBody()isnull(the binary matcher falls through to the single-representation branch, exactly as for an uncompressed request) andgetOrComputeConvertedBodysimply computes — a single response is matched by a single body matcher, so there is no repeated conversion to memoise.
-