2017-02-27 105 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); 
} 
相關問題