简体   繁体   English

Java泛型-需要在对象映射器中泛化A.class

[英]Java generics- Need to generalize A.class in Object mapper

I have a method which maps JSON to Java class. 我有一个将JSON映射到Java类的方法。

mapper.readValue(messageQueue, MessageQueue.class);

I need to use generics such that I can pass classname in method call so that the parsing method can be made generic rather than tighlty coupled. 我需要使用泛型,以便我可以在方法调用中传递类名,以便可以使解析方法成为泛型,而不是tighlty耦合。 Something like maybe: 可能是这样的:

mapper.readValue(messageQueue, Class<T>.class);

You can do it like this. 您可以这样做。

public <T> T deserialize(String message, Class<T> tClass) throws IOException {
    return mapper.readValue(message, tClass);

}

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

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