【问题标题】:Implementing REST API calls in mule - Jersey versus Plain-Old-Mule-HTTP在 mule 中实现 REST API 调用 - Jersey 与 Plain-Old-Mule-HTTP
【发布时间】:2013-10-14 20:08:45
【问题描述】:

我正在尝试确定是否使用--

  1. 带有基于 HTTP 的入站端点的泽西 (JAX-RS)。

  2. 使用基于 HTTP 的入站端点,然后检查 HTTP 标头数据(如 http.method、http.query.params、http.query.string 等)以确定 REST 方法。即实现 REST 的非基于泽西岛的自定义方法。

方法#1的优点

  1. 标准:在 Java 中实现休息服务的基于 JAX-RS 标准的方法。

  2. 文档编制很容易:生成文档非常简单,因为有许多工具使用 JAX-RS 注释来生成文档。

方法#1的缺点

  1. 如果我们必须在 Mule 中使用 Jersey,则 Jersey 方法充当有效负载数据的传递。例子-

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String create(String jsonPayload) {
        logger.debug("Received and added data :" jasonPayload);
        return jsonPayload;
    

    } 在我们的用例中,我们必须将此数据传递给下一个流,在该流中,它要么插入数据库,要么转发到其他 Web 服务。我们不想在这个类中注入特定于 mule 的代码来从 create 方法中调用其他 Mule 流。我们别无选择,只能将有效负载从该方法中传递出来并在 mule 流中处理。

  2. 在 Jersey 处理 create 方法后,它会创建一个封装负载的 Response 对象。如果我们想对有效负载做一些事情,那么我们必须首先从响应对象中提取有效负载。这是不必要的麻烦。

有什么建议、意见、想法吗?

【问题讨论】:

    标签: mule


    【解决方案1】:

    根据大卫的反馈。这是我的实现方式--

    REST 类是 TestAPI --

    @Path("/test")
    public class TestAPI {
    private DBOperations dbo;
    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String create(String jsonPayload) {
        System.out.println("Received and added global attribute :"
                + jsonPayload);
        dbo.create(jsonPayload);
        return jsonPayload;
    }
    

    界面如下--

    public interface DBOperations {
    
      public String create(String json);
      public String read(String json);
      public String update(String json);
      public String delete(String json);
    
    } 
    

    这里是 mule 流程,它显示了 DBOperations 的每个方法如何映射到 mule 配置中的不同流程。

    <flow name="jersey-rest-flow" doc:name="jersey-rest-flow">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8100" doc:name="HTTP" />
        <logger message="Message Received - #[payload]" level="INFO"
            doc:name="Logger" />
        <jersey:resources doc:name="REST">
            <component class="com.test.rest.TestAPI">
                <binding interface="com.test.DBOperations"
                    method="create">
                    <vm:outbound-endpoint exchange-pattern="request-response"
                        path="create-data-vm" />
                </binding>
                <binding interface="com.test.DBOperations"
                    method="read">
                    <vm:outbound-endpoint exchange-pattern="request-response"
                        path="read-data-vm" />
                </binding>
                <binding interface="com.test.DBOperations"
                    method="update">
                    <vm:outbound-endpoint exchange-pattern="request-response"
                        path="update-data-vm" />
                </binding>
                <binding interface="com.test.DBOperations"
                    method="delete">
                    <vm:outbound-endpoint exchange-pattern="request-response"
                        path="delete-data-vm" />
                </binding>
            </component>
        </jersey:resources>
    </flow>
    

    【讨论】:

      【解决方案2】:

      还有第三种选择,如果您不想将自己绑定到 Java 代码 - 它是 REST 路由器模块:
      http://mulesoft.github.io/mule-module-rest-router/mule/rest-router-config.html
      我认为它更适合您.

      更重要的是,几天前我写了一篇关于 Mule 上的 REST 服务的文章,描述了所有这三种方法。包括的例子。可能对你有帮助:
      http://poznachowski.blogspot.com/2013/10/exposing-restful-interface-with-mule-pt1.html
      http://poznachowski.blogspot.com/2013/10/exposing-restful-interface-with-mule-pt2.html

      【讨论】:

        【解决方案3】:

        我们不想在这个类中注入特定于 mule 的代码来从 create 方法中调用其他 Mule 流。我们别无选择,只能将有效负载从该方法中传递出来并在 mule 流中处理。

        我不同意这种说法:组件绑定将 Mule-free 自定义接口注入到您自己的类中。这是我推荐您使用的方法:http://www.mulesoft.org/documentation/display/current/Component+Bindings

        【讨论】:

        • 谢谢大卫。我喜欢这种方法。但是,我面临this 问题。知道如何解决这个问题吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多