Dropwizard资源类调用了另一个资源方法类? [英] Dropwizard resource classes calling another resource method classes?

查看:58
本文介绍了Dropwizard资源类调用了另一个资源方法类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在dropwizard中是否可以从另一个资源类调用另一个资源方法类。

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

我环顾了其他帖子,使用ResourceContext可以从另一个资源类调用get方法,但是也可以使用另一个资源类的post方法。

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.

我们有两个资源类A和B。在类A中,我创建了一些JSON,我想使用B的post方法将该JSON发布到B类。那有可能吗?

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?

推荐答案

是的,资源上下文可用于访问 POST GET 方法从相同或不同资源中的另一个方法获取。

借助 @Context ,您可以轻松访问方法。

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

您现在可以访问以下列方式从资源B 中获取资源A

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

这篇关于Dropwizard资源类调用了另一个资源方法类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆