ballerina - 是否可以为 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 中,请求是基于路径和 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
推荐阅读
- c# - Net Core API:ProducesResponseType 的用途
- shiny - 在同一个事件中闪亮的多个对象Reactive
- java - 错误:不能将原始构造函数引用与构造函数的显式类型参数一起使用
- crystal-lang - 在 Crystal Lang 中获取哈希“字节大小”
- python - SymPy 是否正确解决了这个 ODE?
- ruby - 迭代嵌套哈希以获取字符串数组
- php - 如何在 PHP 中为 LOAD DATA 语句提供文件和目录名称
- python - 在 aiohttp 中使用共享 TCPConnector 时出错
- json - 无法在没有“”符号的情况下快速解析 JSON(手表套件)
- docker - 如何使用 Docker Build 修复 Azure DevOps 构建中的“访问路径 csproj.nuget.g.props 被拒绝”错误?