我从网络服务获取数据。我想创建一个泛型方法来发出请求,并根据我传递的类型作为参数来转换输出。让我们看看代码:
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)
答案 0 :(得分:1)
你几乎得到了它:
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>
(因为您希望列表作为结果)应该可以解决问题。< / p>
答案 1 :(得分:1)
最后。做了一些研究,现在它按照我们的需要工作(对不起,这个对我来说非常个人化[在这里插入愤怒的表情符号])
这就是魔术:
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;
}
}