【问题标题】:Is it possible to define default resources for a Ballerina service?是否可以为 Ballerina 服务定义默认资源?
【发布时间】:2018-05-22 15:39:28
【问题描述】:

想看看能不能暴露 卷曲http://localhost:9090/studentinfo?schoolId=12341324, 其中“studentinfo”是服务路径。

    @http:ServiceConfig { basePath: "/studentinfo" }
    service<http:Service> studentInfo bind studentInfoListener {

            @http:ResourceConfig {
                methods: ["GET"],
                path: "?"
            }
            getStudentBySearch(endpoint client, http:Request req) {

                http:Response response;

                var params = req.getQueryParams();
                var schoolId = <string>params.schoolId;
                var addmissionYear = <string>params.addmissionYear;
            ...
            }
    ...
    }

【问题讨论】:

    标签: ballerina


    【解决方案1】:

    在 Ballerina 中,请求是基于路径和 HTTP 动词分派的。当涉及到默认资源时,路径和动词都不应限制请求。请考虑以下代码 sn-p。

    @http:ResourceConfig {
        path: "/*"
    }
    getStudentBySearch(endpoint client, http:Request req) {
    
        http:Response response;
    
        var params = req.getQueryParams();
        var schoolId = <string>params.schoolId;
        var addmissionYear = <string>params.addmissionYear;
    ...
    }
    

    这里没有特别说明HTTP动词。这意味着任何动词都是允许的。

    当 path 定义为“/*”时,basePath 之后的任何路径段将在没有特定匹配的情况下与之匹配。

    示例网址:

    http://localhost:9090/studentinfo?schoolId=12341324,

    http://localhost:9090/studentinfo/resourcePath?schoolId=12341324

    http://localhost:9090/studentinfo/name -X POST

    【讨论】:

      【解决方案2】:

      只使用“/”作为资源路径应该可以工作。

      @http:ResourceConfig {
          methods: ["GET"],
          path: "/"
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-12
        • 2021-06-23
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        • 2023-01-26
        相关资源
        最近更新 更多