目前这是我的帖子:
private static Response postRequest(String endpoint, ABC request, int expectedStatusCode) {
return extractResponse(buildRequest().body(request).post(endpoint), expectedStatusCode);
}
现在我正在添加另一个帖子端点调用,但请求对象类型不同。像这样:
private static Response postRequest(String endpoint, XYZ request, int expectedStatusCode) {
return extractResponse(buildRequest().body(request).post(endpoint), expectedStatusCode);
}
如何在没有冗余代码的情况下重载请求类型?
感谢。
答案 0 :(得分:1)
使用java generics:
private static <T> Response postRequest(String endpoint, T request, int expectedStatusCode) {...}