Jackson ObjectMapper在反序列化泛型类型时抛出java.lang.ClassCastException:java.util.LinkedHashMap

Jackson ObjectMapper在反序列化泛型类型时抛出java.lang.ClassCastException:java.util.LinkedHashMap,java,json,generics,jackson,Java,Json,Generics,Jackson,我试图使用Jackson ObjectMapper反序列化自定义泛型类型类,但测试引发以下异常。我知道Jackson api无法找到正确的构造类型,但我不知道在我的场景中正确的构造类型是什么。 我试过使用ConstructionCollectionType&其他的很少,但现在它可以工作了。因此,我们将非常感谢您的帮助 java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.test.CsSpace

我试图使用Jackson ObjectMapper反序列化自定义泛型类型类,但测试引发以下异常。我知道Jackson api无法找到正确的构造类型,但我不知道在我的场景中正确的构造类型是什么。 我试过使用ConstructionCollectionType&其他的很少,但现在它可以工作了。因此,我们将非常感谢您的帮助

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.test.CsSpace
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
下面是自定义泛型类型类

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response<T> {

    private int totalResults;
    private List<T> results;

    public int getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(int totalResults) {
        this.totalResults = totalResults;
    }

    public List<T> getResults() {
        return results;
    }

    public void setResults(List<T> results) {
        this.results = results;
    }
}
这是反序列化json的主要业务逻辑

public <T> List<T> getEntity(ConnectorArg arg, String bearerToken, String path, Response<T> gt) throws IOException, ConnectorException {
        String token = BEARER+bearerToken;
        String url = hostName+path;
        ObjectMapper mapper = new ObjectMapper();
        Response res = ClientBuilder
                .newClient()
                .register(JacksonFeature.class)
                .target(url)
                .request(MediaType.APPLICATION_JSON)
                .header(Authorization, token)
                .post(Entity.json(arg));
        if(res.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
            CsSpaceResponse<T> csSpaceRes = mapper.readValue(res.readEntity(String.class), gt.getClass()); 
            //throws the exception above line. Tried mapper.readValue(res.readEntity(String.class), mapper.getTypeFactory().constructCollectionType(List.class, gt.getClass()));
            return csSpaceRes.getResults();
        }else {
            throw new ConnectorException("Exception while connecting to "+url);
        }
    }

我在这里找到了一个类似的答案。
public <T> List<T> getEntity(ConnectorArg arg, String bearerToken, String path, Response<T> gt) throws IOException, ConnectorException {
        String token = BEARER+bearerToken;
        String url = hostName+path;
        ObjectMapper mapper = new ObjectMapper();
        Response res = ClientBuilder
                .newClient()
                .register(JacksonFeature.class)
                .target(url)
                .request(MediaType.APPLICATION_JSON)
                .header(Authorization, token)
                .post(Entity.json(arg));
        if(res.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
            CsSpaceResponse<T> csSpaceRes = mapper.readValue(res.readEntity(String.class), gt.getClass()); 
            //throws the exception above line. Tried mapper.readValue(res.readEntity(String.class), mapper.getTypeFactory().constructCollectionType(List.class, gt.getClass()));
            return csSpaceRes.getResults();
        }else {
            throw new ConnectorException("Exception while connecting to "+url);
        }
    }
{
        "totalResults": 2,
        "results": [{
            "id": "1908",
            "Building_ID": "1234"
        }, {
            "id": "1909",
            "Building_ID": "12347"
        }]
    }