0
我想知道是否可以在dropwizard中從另一個資源類中調用另一個資源方法類。Dropwizard資源類調用另一個資源方法類?
我環顧了其他帖子,使用ResourceContext允許從另一個資源類調用get方法,但也可以使用其他資源類的post方法。
假設我們有兩個資源類A和B.在類A中,我創建了一些JSON,並且我想使用B的post方法將該JSON發佈到B類。這可能嗎?
我想知道是否可以在dropwizard中從另一個資源類中調用另一個資源方法類。Dropwizard資源類調用另一個資源方法類?
我環顧了其他帖子,使用ResourceContext允許從另一個資源類調用get方法,但也可以使用其他資源類的post方法。
假設我們有兩個資源類A和B.在類A中,我創建了一些JSON,並且我想使用B的post方法將該JSON發佈到B類。這可能嗎?
是,資源上下文可以用於從相同或不同資源中的另一種方法訪問POST
和GET
方法。
在@Context
的幫助下,您可以輕鬆訪問這些方法。
@Path("a")
class A{
@GET
public Response getMethod(){
return Response.ok().build();
}
@POST
public Response postMethod(ExampleBean exampleBean){
return Response.ok().build();
}
}
現在,您可以從Resource B
訪問下列方式Resource A
的方法。
@Path("b")
class B{
@Context
private javax.ws.rs.container.ResourceContext rc;
@GET
public Response callMethod(){
//Calling GET method
Response response = rc.getResource(A.class).getMethod();
//Initialize Bean or paramter you have to send in the POST method
ExampleBean exampleBean = new ExampleBean();
//Calling POST method
Response response = rc.getResource(A.class).postMethod(exampleBean);
return Response.ok(response).build();
}
}
你想從A調用一個B的方法嗎?當然,您可以像其他方法調用一樣執行該操作,但是您需要在A中擁有B實例。如果您想對另一個URL(B)進行HTTP POST調用,則可以使用httpclient(球衣或例如apache httpclient)。或者乾脆你可以有一個重定向http://stackoverflow.com/questions/20709386/dropwizard-how-to-do-a-server-side-redirect-from-a-view – gaganbm