问题描述
我正在尝试编写一个扩展程序,我可以在其中将请求和响应存储在数据库中。我扩展了 PostActionServe,现在有两个问题
-
启动模拟服务器后,我向 http://localhost:8089/__admin/mappings 提交了 post 请求,但是 doAction 或 doGlobalAction 都没有被调用。
-
现在,如果我到达终点 /some/thing,那么只会调用 doGlobal Action。
我希望在我向映射 API 提交请求的那一刻,它应该调用此扩展程序,并且我可以将请求和响应存储在数据库中。
下面是代码
导入 com.github.tomakehurst.wiremock.wiremockServer;
导入静态com.github.tomakehurst.wiremock.core.wiremockConfiguration.options; 导入静态 com.github.tomakehurst.wiremock.core.wiremockConfiguration.wiremockConfig;
public class Main {
public static void main(String... args) {
wiremockServer wiremockServer = new wiremockServer(wiremockConfig()
.extensions(new RequestRecorder()).port(8089)); //No-args constructor will start on port 8080,no HTTPS
wiremockServer.start();
}
}
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
public class RequestRecorder extends PostServeAction {
public static final String EXTENSION_NAME = "db-request-recorder";
@Override
public void doAction(final ServeEvent serveEvent,final Admin admin,final Parameters parameters) {
System.out.println(serveEvent.getRequest());
System.out.println(serveEvent.getResponseDeFinition());
}
@Override
public void doGlobalAction(ServeEvent serveEvent,Admin admin) {
System.out.println(serveEvent.getRequest());
System.out.println(serveEvent.getResponseDeFinition());
}
@Override
public String getName() {
return EXTENSION_NAME;
}
}
class MainTest {
public static void main(String... aergs) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://localhost:8089/__admin/mappings");
StringEntity params = new StringEntity("{\n" +
" \"request\": {\n" +
" \"method\": \"GET\",\n" +
" \"url\": \"/some/thing\"\n" +
" },\n" +
" \"response\": {\n" +
" \"status\": 200,\n" +
" \"body\": \"Hello World!\",\n" +
" \"headers\": {\n" +
" \"Content-Type\": \"text/plain\"\n" +
" }\n" +
" }\n" +
"}");
request.addHeader("content-type","application/x-www-form-urlencoded");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
} catch (Exception ex) {
} finally {
// @Deprecated httpClient.getConnectionManager().shutdown();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)