Dropwizard resource classes calling another resource method classes?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 12:50:06

问题


I was wondering if it was possible in dropwizard to call another resource method class from a different resource class.

I looked around other posts and using ResourceContext allows one to call a get method from another resource class, but it also possible to use a post method from another resource class.

Let say we have two resource classes A and B. In class A, I have created some JSON and I want to post that JSON to B class using B's post method. Would that be possible?


回答1:


Yes, Resource Context can be used to access both POST and GET methods from another method in same or different resource.
With the help of @Context, you can easily access the methods.

@Path("a")
class A{
    @GET
    public Response getMethod(){
        return Response.ok().build();
    }
    @POST
    public Response postMethod(ExampleBean exampleBean){
        return Response.ok().build();
    }
}

You can now access the methods of Resource A from Resource B in the following manner.

@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();
    }
}


来源:https://stackoverflow.com/questions/40430666/dropwizard-resource-classes-calling-another-resource-method-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!