我们应该在最新版本(3.0.x)Resteasy中使用ClientResponse和GenericType?

时间:2016-03-30 20:48:11

标签: resteasy deprecated

我正在开发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();
    }

请指导。 最新版本中以下两行的替换是什么?我如何获得学生对象列表?

enter image description here

编辑-1:我在下面尝试过,但是出现以下错误: Students.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)

2 个答案:

答案 0 :(得分:4)

Resteasy Client-API已marked deprecated,因为JAX-RS标准化了Client-API。您现在可以使用等效的<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="result result1"></div> <div class="result result2"></div>类:

javax.ws.rs

答案 1 :(得分:2)

以下是查询参数和HTTP响应代码检查的示例,这对于快速失败和避免来自JSON映射器的堆栈跟踪非常方便,通常隐藏真正的原因:

Response response = null;
try {
  response = target.path("apiMethod")
                .queryParam("stringParam", "test")
                .queryParam("booleanParam", Boolean.FALSE)
                .request().accept(MediaType.APPLICATION_JSON).get();

  if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) { // HTTP 400
    throw new BadRequestException("invalid parameters!");
  } else if (response.getStatus() == 
    Response.Status.NOT_FOUND.getStatusCode()) {  // HTTP 404
        throw new NotFoundException("resource was not found on server!");
  }

    List<MyPojo> result = response.readEntity(new GenericType<List<MyPojo>>(){});
    return result;
} finally {
  if (response != null) {
    response.close();
  }
}