JAVA Genericity Foo <bar> .class

时间:2017-07-08 10:19:25

标签: java generics

我有一个通用的:

public class GenericResponseEntities<T> extends BSResponseEntity {

    private List<T> returnedObject;

    /**
     * @return the returnedObject
     */
    public List<T> getReturnedObject() {
    return returnedObject;
    }

    /**
     * @param returnedObject
     *            the returnedObject to set
     */
    public void setReturnedObject(List<T> returnedObject) {
    this.returnedObject = returnedObject;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
    return "GenericResponseEntities [returnedObject=" + returnedObject + "]";
    }
}

我这样用:

public static <T> List<T> getGenericFromUrl(String url) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<GenericResponseEntities<T>> responseEntity;
List<T> list = new ArrayList<T>();

try {
    responseEntity = restTemplate.getForEntity(url, GenericResponseEntities<T>.class);

    if (responseEntity.getBody().getCode().equals(HttpStatus.OK.toString())) {
    list.addAll(responseEntity.getBody().getReturnedObject());
    }
} catch (Exception e) {
    e.printStackTrace();
}

return list;
}

我在ligne上有这些错误: enter image description here

enter image description here

我的问题是如何获得GenericResponseEntities.class类?

我可以通过使用特定的响应实体(FooResponseEntity,BarResponseEntity ...)来避免这些错误。通过这个解决方案,我将拥有特定服务器响应的特定响应实体。

由于

Msf vtp 如果我删除签名中的<T>,则会出现此错误:

enter image description here

0 个答案:

没有答案