You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Spring Cloud Contract存根仅精确匹配请求且无需配置优先级?

How to Get Exact Request Matching for Spring Cloud Contract Stubs (No Priority Config Needed)

Absolutely! You don't have to mess with priority settings to ensure your more specific stubs (like /users?id=1&id=2) get matched over broader ones (like /users). The key is to enforce exact matching rules for your contracts, so Spring Cloud Contract knows to prioritize requests that fully match the stub's defined criteria.

1. Define Precise Query Parameter Matching in Your Contracts

Instead of leaving query parameters implicit, explicitly define how they should be matched in each stub. This removes ambiguity and tells the stub runner exactly what to look for.

Example (Groovy DSL)

For the /users?id=1&id=2 stub:

Contract.make {
    request {
        method GET()
        url "/users" {
            queryParameters {
                // Explicitly match the exact id values (supports multi-value params)
                parameter "id", equalTo("1")
                parameter "id", equalTo("2")
            }
        }
    }
    response {
        status OK()
        body("""{"users": [1, 2]}""")
    }
}

For the /users stub (no query params allowed):

Contract.make {
    request {
        method GET()
        url "/users" {
            queryParameters {
                // Ensure the `id` parameter is completely missing
                parameter "id", missing()
            }
        }
    }
    response {
        status OK()
        body("""{"users": []}""")
    }
}

Example (Java DSL)

If you prefer Java:
For the parameterized stub:

Contract.make(c -> {
    c.request(r -> {
        r.method(HttpMethod.GET);
        r.url("/users", u -> {
            u.queryParameter("id", "1");
            u.queryParameter("id", "2");
        });
    });
    c.response(r -> {
        r.status(HttpStatus.OK);
        r.body("{\"users\": [1,2]}");
    });
});

For the no-parameter stub:

Contract.make(c -> {
    c.request(r -> {
        r.method(HttpMethod.GET);
        r.url("/users", u -> {
            u.queryParameter("id", Matchers.missing());
        });
    });
    c.response(r -> {
        r.status(HttpStatus.OK);
        r.body("{\"users\": []}");
    });
});

2. Enable Global Exact Matching Strategy

To make this behavior consistent across all your stubs, set the global matching strategy to EXACT in your configuration. This tells Spring Cloud Contract to only match stubs where every part of the request (URL path, query params, headers, body) exactly matches the stub's definition.

Application.yml Configuration

spring:
  cloud:
    contract:
      stubrunner:
        matching-strategy: EXACT

Application.properties Configuration

spring.cloud.contract.stubrunner.matching-strategy=EXACT

Why This Works

By default, Spring Cloud Contract uses a DEFAULT matching strategy that can favor shorter URL paths over more specific ones. With the above setup:

  • The explicit query parameter rules eliminate ambiguity between your two stubs.
  • The EXACT matching strategy ensures that only requests that perfectly match all criteria of a stub will trigger it.

Now when you send a request to /users?id=1&id=2, it will only match the stub that explicitly defines those query parameters—no priority tweaks required.

内容的提问来源于stack exchange,提问作者geonikpal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:52:35