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


这可能吗?

任何帮助表示赞赏。

最佳答案

由于您不需要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);
}

关于java - 如何使用给定的类类型实例化HttpEntity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54588036/

10-10 23:30