2014-12-02 24 views
1

我必須建模剩餘的服務操作,我嘗試創建一個URI來命中Java內部類。讓我知道這是否可以使用Rest Web Service來實現?我正在使用我的服務輕鬆休息。Rest服務URI映射到Java內部類

編輯:(使用的答案從peeskillet提供的代碼)

javax.ws.rs.ProcessingException: Unable to invoke request 
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287) 
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407) 

Caused by: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8081 refused 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190) 
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) 
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:643) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) 
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283) 
... 26 more 
Caused by: java.net.ConnectException: Connection refused 
+0

很抱歉,但你的問題是有點不清楚。請張貼一些代碼來演示這個問題,以及你想要的輸出結果。 – 2014-12-02 10:33:02

+0

如果說/ rest/message/123是我獲取123 ID數據的其餘URI。現在我想創建一個URI,這樣我就可以擁有名爲Comment內部消息的內部類。所以我想知道如果我可以創建一個像/ rest/message/comment/12這樣的服務。如果我聽起來很清楚,請告訴我。 – skanda 2014-12-02 10:58:11

+0

所以評論只是一個模型類的權利(建模傳入的JSON)?不是資源類? – 2014-12-02 11:30:17

回答

2

請告知,如果資源類可以作出內部類與其他類

是的,它是可能,但我們不能只是註釋內部類路徑@Path,並期望呼叫到達那裏。我們需要使用「子資源定位器」。基本上,它可以是一種只用@Path註釋的方法,它將返回內部資源類的一個實例。像

@Path("/messages") 
public class MessageResource { 

    @GET 
    @Path("/{id}") 
    public Response getMessage(@PathParam("id") int id) { 
     return Response.ok("messages, id: " + id).build(); 
    } 

    @Path("/{id}/comments") 
    public CommentsResource getComments() { 
     return new CommentsResource(); 
    } 

    public class CommentsResource { 

     @GET 
     @Path("/{id}") 
     public Response getComment(@PathParam("id") int id) { 
      return Response.ok("Hello Sub-Resource Locators").build(); 
     } 
    } 
} 

的東西,我們可以測試它

@Test 
public void testResteasy() { 
    WebTarget target = client.target(TestPortProvider.generateURL(BASE_URI)) 
      .path("messages").path("1234").path("comments").path("5678"); 
    Response response = target.request().get(); 
    System.out.println("Status:" + response.getStatus()); 
    System.out.println("Response: " + response.readEntity(String.class)); 
    response.close(); 
} 

而且我們會得到響應Hello Sub-Resource Locators


進一步閱讀


編輯:

基礎上您所提供的錯誤,TestPortProvider將使用8081端口,並與http://localhost:8081開始新的URI。您將傳遞給gererateURL的內容將被追加。通常我會在嵌入式服務器測試中使用它。但是,如果你正在運行實際的服務器,這將極有可能在8080端口上,我建議剛好路過字符串基本URL,而不是,是這樣的:

WebTarget target = client.target("http://localhost:8080/MyApp/rest") 
     .path("messages").path("1234").path("comments").path("5678"); 
+0

謝謝!這是我正在尋找,但編譯測試代碼時,我得到錯誤張貼。請指教。 – skanda 2014-12-03 06:24:01

+0

對不起,測試代碼'TestPortProvider'是我喜歡使用的測試框架的一部分。你可以用'http:// localhost:8080/api'完全替換'TestPortProvider.generateURL(BASE_URI)'。剩餘的URI通過'path'方法鏈接以編程方式設置。所以最終的URI將會是'http:// localhost:8080/api/messages/1234/comments/5678'。您需要自行確定基數是多少 – 2014-12-03 06:26:10

+0

您也可以嘗試在瀏覽器中輸入網址以查看其工作情況。 – 2014-12-03 06:30:58