简体   繁体   English

Java通用地图未在运行时转换为Json会话的正确类型

[英]Java Generic Map not converted to proper type in runtime for Json converstion

I have one method which works fine when i use actual class, but don't give expected output when using generics. 我有一种方法可以在使用实际类时正常工作,但在使用泛型时却无法提供预期的输出。

Below is the method which works fine when using ABC class 下面是使用ABC类时可以正常工作的方法

public static List<ABC> getMemberViewRepresentation(Response response) throws JSONException, IOException {
        JSONObject jsonObj = new JSONObject(response.readEntity(String.class));
        ObjectMapper mapper = new ObjectMapper();
        JSONObject memberViewObj = (JSONObject)jsonObj.get("members");
        TypeReference<HashMap<String, ABC>> typeRef = new TypeReference<HashMap<String, ABC>>() {};        
        Map<String, ABC> map = mapper.readValue(memberViewObj.toString(), typeRef);
        return new ArrayList<>(map.values());
    }

This methods gives proper output which has list of type ABC. 此方法将给出具有ABC类型列表的正确输出。

But i want to write code such that i pass class dynamically so that this method can be used by anyone. 但是我想编写代码,以便我动态地传递类,以便任何人都可以使用此方法。 So i have written below code, also i tried other way but none of them seems to work. 所以我写了下面的代码,我也尝试了其他方法,但是它们似乎都不起作用。

public static<T> List<T> getMemberViewRepresentation(Response response) throws JSONException, IOException {
        JSONObject jsonObj = new JSONObject(response.readEntity(String.class));
        ObjectMapper mapper = new ObjectMapper();
        JSONObject memberViewObj = (JSONObject)jsonObj.get("members");
        TypeReference<HashMap<String, T>> typeRef = new TypeReference<HashMap<String, T>>() {};        
        Map<String, T> map = mapper.readValue(memberViewObj.toString(), typeRef);
        return new ArrayList<>(map.values());
    }
I am calling in this way
List<ABC> nodes = ResponseUtil.getMemberViewRepresentation(response);

But the output of above method is not same. 但是以上方法的输出是不一样的。 List is not of type ABC 清单不是ABC类型

@JsonIgnoreProperties(ignoreUnknown = true)
public class ABC {
    @JsonProperty("id")
    private int id;
    @JsonProperty("uid")
    private String uid;

    public int getId() {
        return id;
    }

    public String getUid() {
        return uid;
    }
}

Any idea how to keep the method dynamic such that i can pass Class details separately 任何想法如何使方法保持动态,以便我可以分别传递类详细信息

I think, what you need is: 我认为,您需要的是:

List<ABC> nodes = ResponseUtil.<ABC>getMemberViewRepresentation(response);

...to infer the generic parameter(s) into a static method (WITHOUT typed parameter(s)). ...以将通用参数推断为静态方法(没有输入参数)。

Note the extra <ABC> before method invocation! 注意方法调用之前的额外<ABC> Normally you don't need it, because a regular generic method goes like: public static<T> void foo(T someInput) {...} ...and providing someInput , T is known/easy to infer at runtime. 通常,您不需要它,因为常规的通用方法类似于: public static<T> void foo(T someInput) {...} ...并提供someInputT是已知的/易于在运行时推断。 In your case Response has nothing to do with ABC ...so can't be inferred (with anything else than ? ). 在您的情况下, ResponseABC无关...因此无法推断(与?无关)。

The first method works as expected, because there is no "generic method", while the "class generic type" seems to be inferred correctly (somewhere else in your code/configuration). 第一种方法可以正常工作,因为没有“泛型方法”,而“类泛型类型”似乎可以正确推断(在代码/配置中的其他地方)。

The second doesn't, because the "generic method type" is missing (and i assume inferred as <?> respectively <java.lang.Object> ...so you get a List<Object> returned and assigned to your List<ABC> , which is not as expected, but at least "fail free"). 第二个没有,因为缺少“通用方法类型”(我假设分别推断为<?> <java.lang.Object> ...因此您得到一个返回的List<Object>并分配给您的List<ABC> ,这不是预期的,但至少是“无故障”)。

You can argument, that we can "infer" ABC from the return type (you expect a List<ABC> , where a List<T> is delivered), but unfortunately this is not how it works /this information is not available at that point/needs deeper study. 您可以争论,我们可以从返回类型“推断” ABC (您希望获得List<ABC> ,其中交付了List<T> ),但是不幸的这不是它的工作方式 /该信息尚不可用点/需要更深入的研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM