我想创建一个我传递的类类型的HttpEntity。我正在尝试执行以下操作:
private <T> HttpEntity<clazz> createRequestEntity(String
cl, String co, String con, T body, Class<?> clazz)
{
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set(Constants.XCL, cl);
httpHeaders.set(Constants.XCO, co);
httpHeaders.set(Constants.XCON, con);
HttpEntity<clazz> httpEntity = new HttpEntity<Object>(body, httpHeaders);
return httpEntity;
}
这可能吗?
感谢您的帮助。
答案 0 :(得分:3)
由于您不需要clazz
类型,因此它不必成为参数的一部分。代码可能是这样的:
private <T> HttpEntity<T> createRequestEntity(String cl, String co, String con, T body)
{
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(body, httpHeaders);
}