这是我模拟服务的实用程序类
public class MockService {
public static void bootUpMockServices() throws IOException {
String orderServiceSpecification = readFile("mappings/orderServicesSpecifications.json", Charset.defaultCharset());
String singleOrder = readFile("mappings/singleOrder.json", Charset.defaultCharset());
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/orders"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withBody(orderServiceSpecification)));
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/orders/1"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withBody(singleOrder)));
}
public static String readFile(String path, Charset encoding)
throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
正如您所看到的,我正在模拟一个 GET 调用/orders
(包含所有订单)并使用正文响应并将所有订单保存在一个 json 文件中。
我还通过 GET 调用调用单个订单/orders/1
。我正在使用文件中的 JSON 对象对其进行响应。但我希望它是动态的。就像当我点击它时一样orders/30
,我应该动态地获取订单id=30
并渲染它。