如何使用给定的类类型实例化HttpEntity

时间:2019-02-08 07:53:00

标签: java

我想创建一个我传递的类类型的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;
}

这可能吗?

感谢您的帮助。

1 个答案:

答案 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);
}