我已经启动了Stub Runner Server Fat Jar(通过链接https://search.maven.org/remote_content?g=org.springframework.cloud&a=spring-cloud-contract-stub-runner-boot&v=2.0.1.RELEASE下载jar文件,并在控制台java -jar spring-cloud-contract-stub-runner-boot-2.0.1.RELEASE.jar中执行命令- -stubrunner.ids = com.example:用户:+ :: stubs:8555 --stubrunner.stubsMode = LOCAL) 作为结果,在控制台中,我看到Stub Runner Server在端口8080上成功启动,并以以下方式查看我合同的JSON:
... WireMock : Received request to /mappings with body {
"id" : "75587005-85c4-4cf9-a07c-2d16c2ede8b9",
"request" : {
"url" : "/exampleUrl",
"method" : "GET"
},
"response" : {
"status" : 200,
"transformers" : [ "response-template" ]
},
"uuid" : "75587005-85c4-4cf9-a07c-2d16c2ede8b9"
}
(以及许多其他合同的JSON)
此外,当我转到链接http://localhost:8080/stubs/
时,我会得到响应{"com.example:users:19.34.1214-SNAPSHOT:stubs":8555}
那么如何通过链接在浏览器中获取合同的JSON(如上所示)?
我创建了具有StubFinder类的应用程序(在端口8096上),但是我不知道如何将应用程序与端口8080上的stubrunner连接以查找合同。还是这是错误的策略,我需要使用其他方法吗? 我已经阅读了很多次文档,却没有找到如何做这样的事情。
主类:
package com.exampleproject.stub_runner_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.contract.stubrunner.server.EnableStubRunnerServer;
@SpringBootApplication
@EnableStubRunnerServer
public class StubRunnerApplication {
public static void main(String[] args) {
SpringApplication.run(StubRunnerApplication.class, args);
}
}
stubrunner.yml
stubrunner:
stubsMode: LOCAL
ids: com.example:users:+:stubs:8555
用于通过REST获取合同的控制器
package com.exampleproject.stub_runner_server.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.contract.stubrunner.StubFinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stubs")
public class StubsController {
private StubFinder stubFinder;
@Autowired
public StubsController(StubFinder stubFinder) {
this.stubFinder = stubFinder;
}
@GetMapping("/s")
public String getStubs() {
return stubFinder.getContracts().toString();
}
}