WireMock配置异常:已定义请求仍被代理,调整优先级无效
WireMock映射规则失效问题排查与修复
核心问题:JSON语法错误
你提供的映射配置里,第一个映射规则的request对象中,urlPathPattern键值对末尾多了一个逗号,这会导致整个JSON配置解析失败。WireMock加载配置时如果遇到语法错误,会忽略所有自定义映射,直接触发兜底的代理规则,这就是所有请求都被转发到http://localhost:922的根本原因。
修复后的第一个映射规则片段:
{ "priority": 1, "request": { "method": "GET", "urlPathPattern": "/container/epsSelfService/customers/([a-zA-Z1-9]*)/cashDepositTotals/([a-zA-Z1-9]*)" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "bodyFileName": "depositlimitpersonalresponse.json" } }
额外优化建议
- 正则表达式补全:URL正则
[a-zA-Z1-9]*漏掉了数字0,建议改为[a-zA-Z0-9]*,避免匹配不到包含0的路径参数。 - 兜底规则限制请求方法:当前兜底规则未指定
method,会匹配所有请求方法。如果仅需代理非GET请求,可添加"method": "ANY",避免干扰正常映射逻辑。 - 验证文件路径:确保
bodyFileName指定的JSON文件放在WireMock的__files目录下,否则会返回404错误。
完整修复后的配置
{ "mappings": [ { "priority": 1, "request": { "method": "GET", "urlPathPattern": "/container/epsSelfService/customers/([a-zA-Z0-9]*)/cashDepositTotals/([a-zA-Z0-9]*)" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "bodyFileName": "depositlimitpersonalresponse.json" } }, { "priority": 2, "request": { "method": "GET", "urlPathPattern": "/container/epsSelfService/chubStartV1" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "bodyFileName": "startclearinghub.json" } }, { "priority": 3, "request": { "method": "GET", "urlPathPattern": "/container/epsSelfService/chubSubmitV1" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "bodyFileName": "submitclearinghub.json" } }, { "priority": 4, "request": { "method": "GET", "urlPathPattern": "/container/epsSelfService/chubValidateV1" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "bodyFileName": "validateclearinghub.json" } }, { "priority": 5, "request": { "method": "GET", "urlPathPattern": "/container/epsSelfService/chubCompleteV1" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "bodyFileName": "completeclearinghub.json" } }, { "priority": 100, "request": { "method": "ANY", "urlPathPattern": ".*" }, "response": { "proxyBaseUrl": "http://localhost:922" } } ] }
内容的提问来源于stack exchange,提问作者Satya Prakash Mishra
相关产品推荐
相关产品推荐

