2017-02-27 103 views
1

我有一个运行在WildFly 10上的Java EE应用程序。此应用程序使用Jersey并且在使用REST客户端测试时运行良好。使用JUnit和Jersey客户端测试JAX-RS应用程序

我写了一个JUnit测试,它使用Jersey Client API向上述应用程序发出请求。当我运行它,我得到如下:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error 
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:209) 
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:174) 
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:473) 
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:165) 

在跟踪下一行引用下面的request()电话:

@Test 
public void test() { 
    Client client = ClientBuilder.newClient(); 
    WebTarget target = client.target("http://localhost:8080/myapp/webapi"); 
    target.path("/users"); 
    String response = target.request("application/json").get(String.class); 
    assertEquals("test", response); 
} 

任何想法?

+0

它说埃罗r在服务器端不在客户端,很可能你没有将所有必需的参数传递给服务器 – hoaz

回答

1

你的问题来自于服务器端(参见错误500),如果你想自己去查,打开浏览器并转到的网址:http://localhost:8080/myapp/webapi

而且Javadoc中发现WebTarget.path( )返回一个新的WebTarget

https://jersey.java.net/apidocs/2.22/jersey/javax/ws/rs/client/WebTarget.html#path(java.lang.String)

我相信,在你的代码你真正想要做的是:

@Test 
public void test() { 
    Client client = ClientBuilder.newClient(); 
    WebTarget target = client.target("http://localhost:8080/myapp/webapi"); 
    WebTarget targetUpdated = target.path("/users"); 
    String response = targetUpdated.request("application/json").get(String.class); 
    assertEquals("test", response); 
} 
相关问题