首页 > 解决方案 > 是否可以为 Ballerina 服务定义默认资源?

问题描述

试图查看我是否可以公开 curl 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

解决方案


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

@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


推荐阅读