我正在开发Resteasy
。我将应用程序的Maven依赖关系从2.2.x
迁移到3.0.x
,突然我看到大多数API都已弃用。因此,这种迁移对我的代码和测试用例产生了影响,因为它在我的整个代码中简而言之。
我以测试用例为例:
具有较早版本的测试用例(最新版本已废弃,如链接中所述: ClientRequestFactory RestEasy Deprecated... Any other RestEasy alternative ? ):
import org.jboss.resteasy.util.GenericType;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
@Test
public void testGetStudent() throws Exception{
String str = "http://localhost:8080/RESTfulExample/rest/restwebservice/list";
ClientRequest request = new ClientRequest(str);
ClientResponse<List<Student>> response = request.get(new GenericType<List<Student>>(){});
List<Student> students = response.getEntity();
System.out.println("Size : "+students.size());
}
所以我重构了测试用例
@Test
public void testGetStudents(){
final String str = "http://localhost:8080/RESTfulExample/rest/restwebservice/list";
Client client = ClientBuilder.newClient();
// Client client = ClientBuilder.newBuilder().build(); // This also works, OR
Response response = client.target(str).request().get();
// This will gives us whole XML output
String s = response.readEntity(String.class);
System.out.println(s);
**What is the replacement of below two lines in latest version? How I can get List of Student Object ?**
// ClientResponse<List<Student>> response = request.get(new GenericType<List<Student>>(){});
// List<Student> students = response.getEntity();
}
请指导。 最新版本中下面两行的替换是什么?如何获取学生对象列表?
编辑1:我尝试以下,但给出以下错误:
学生.java
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "students")
public class Students {
@XmlElement(name="student")
private ArrayList<Student> users;
public ArrayList<Student> getUsers() {
return users;
}
public void setUsers(ArrayList<Student> users) {
this.users = users;
}
}
但是它给我错误:
@Test
public void testGetStudents(){
final String str = "http://localhost:8080/RESTfulExample/rest/restwebservice/list";
Client client = ClientBuilder.newClient();
// Client client = ClientBuilder.newBuilder().build(); // This also works, OR
Response response = client.target(str).request().get();
// This will gives us whole XML output
String stringOutput = response.readEntity(String.class);
System.out.println("STATUS : "+response.getStatus());
System.out.println(stringOutput);
Students students = response.readEntity(Students.class);
System.out.println("Size : "+students.getUsers().size());
}
java.lang.IllegalStateException: RESTEASY003765: Response is closed.
at org.jboss.resteasy.specimpl.BuiltResponse.abortIfClosed(BuiltResponse.java:256)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.abortIfClosed(ClientResponse.java:328)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:152)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:219)
at com.mkyong.rest.test.RestEasySampleTest.testGetStudents(RestEasySampleTest.java:46)
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:497)
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:50)
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:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
最佳答案
由于JAX-RS标准化了客户端API,因此Resteasy客户端API已被称为marked deprecated。现在,您可以使用等效的javax.ws.rs
类:
javax.ws.rs.client.Client client = javax.ws.rs.client.ClientBuilder.newClient();
javax.ws.rs.client.WebTarget target = client.target("someUrl");
List<Student> students =
target.request().get(new javax.ws.rs.core.GenericType<List<Student>>() {});
关于resteasy - 在最新版本(3.0.x)Resteasy中,我们应该对ClientResponse和GenericType使用什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36319394/