[英]return generic list type according the parameter
我从网络服务获取数据。 我想创建一个通用方法来发出请求,并根据我作为参数传递的类型来转换输出。 让我们看一下代码:
public <T> Class getAPI(URL url, Class <T> clazz)
{
// GET
ObjectMapper mapper = new ObjectMapper();
List<clazz> objs = mapper.readValue(url, new TypeReference<List<clazz>>(){});
return objs;
}
尚无成功...
可能吗?
编辑
这就是我所说的方法:
URL url = new URL("http://localhost:8080/...");
Util u = new Util();
List<className> objs = u.getAPI(url, className.class);
System.out.println(className.get(0).getId());
ClassCastException的Stacktrace
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to pkg.className
at GetTest.test(GetTest.java:31)
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)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
您几乎明白了:
public <T> List<T> getAPI(URL url, Class <T> clazz)
{
// GET
ObjectMapper mapper = new ObjectMapper();
List<T> objs = mapper.readValue(url, new TypeReference<List<T>>(){});
return objs;
}
没有测试,但用clazz
有T
,使返回类型List<T>
因为你想有一个列表的结果)应该做的伎俩。
最后。 做了一些研究,现在可以根据需要运行(对不起,这个对我来说非常个性化(在这里插入愤怒的表情符号))
这就是魔术:
public <T> List<T> getAPI(URL url, Class <T> clazz) throws JsonParseException, JsonMappingException, IOException {
// GET
ObjectMapper mapper = new ObjectMapper();
List<T> objs = mapper.readValue(url, TypeFactory.defaultInstance().constructParametrizedType(ArrayList.class, List.class, clazz));
return objs;
}
这里的完整代码(TestData仍然相同):
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
public class Util {
// Just as posted by prior post:
public <T> List<T> getAPI(URL url, Class <T> clazz) throws JsonParseException, JsonMappingException, IOException {
// GET
ObjectMapper mapper = new ObjectMapper();
List<T> objs = mapper.readValue(url, TypeFactory.defaultInstance().constructParametrizedType(ArrayList.class, List.class, clazz));
return objs;
}
// Test all together
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
URL url = new URL("https://jsonplaceholder.typicode.com/comments");
Util u = new Util();
List<TestData> objs = u.getAPI(url, TestData.class);
System.out.println(objs.get(0).getId());
}
}
---不是那么原始的帖子---
好吧,猜想我又失败了一次...实际上,仿制药工作得很好,但杰克逊不喜欢我们的仿制药,如此处指出: https : //stackoverflow.com/a/6078477/2355392
第二种方法可能是一个很好的起点,但是当我尝试这种方法时,我的类路径中没有TypeFactory.genericType()
,也许是错误的杰克逊版本...
下面的屏幕快照是可以的,直到您让SysOut运行,它都会与您上面提到的ClassCastException
崩溃(多么令人尴尬...)
-原始帖子-
因此,您在这里:
import java.io.IOException;
import java.net.URL;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Util {
// exactly like posted in my prior answer (despite exceptionhandling):
public <T> List<T> getAPI(URL url, Class <T> clazz) throws JsonParseException, JsonMappingException, IOException {
// GET
ObjectMapper mapper = new ObjectMapper();
List<T> objs = mapper.readValue(url, new TypeReference<List<T>>(){});
return objs;
}
// Test all together
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
// thanks for this URL!
URL url = new URL("https://jsonplaceholder.typicode.com/comments");
Util u = new Util();
List<TestData> objs = u.getAPI(url, TestData.class);
System.out.println(objs.get(0).getId());
}
}
我使用的TestData
PoJo:
public class TestData {
private String name, email, body;
private int postId, id;
// generated by IDE, i'm lazy AF
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public int getPostId() {
return postId;
}
public void setPostId(int postId) {
this.postId = postId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.