如何在Wiremock Standalone中配置全局默认响应头?
WireMock Docker场景统一设置全局Content-Type响应头
以下是几种无需在每个映射中重复配置的可行方案:
方案1:启动时通过命令行参数全局配置
直接在Docker启动命令中添加--global-response-header参数,指定默认的Content-Type头,这是最简便的方法:
docker run -p 8080:8080 -v $(pwd)/wiremock:/home/wiremock wiremock/wiremock:latest --global-response-header "Content-Type:application/json"
该参数会为所有响应自动添加Content-Type: application/json头,不会覆盖映射中已自定义的同名字段。
方案2:通过全局映射文件配置
在本地WireMock映射目录(挂载到容器/home/wiremock/mappings的目录)下创建__global.json文件,内容如下:
{ "priority": 100, "request": { "method": "ANY", "urlPattern": ".*" }, "response": { "headers": { "Content-Type": "application/json" }, "transformers": ["response-template"], "transformerParameters": { "skipBody": true } } }
- 优先级设为100(最低),确保不会覆盖你自定义的高优先级映射逻辑
- 匹配所有请求和URL,自动注入Content-Type头
- 借助
response-template转换器避免覆盖原有映射的响应内容
注意事项
- 确保使用的WireMock镜像版本支持上述特性(建议使用v2.35.0及以上版本)
- 如果个别映射需要自定义Content-Type,直接在对应映射的
response.headers中配置即可,会覆盖全局设置
内容的提问来源于stack exchange,提问作者Thomas Levesque
相关产品推荐
相关产品推荐

