You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ballerina中@http:ServiceConfig注解误用导致编译错误求助

问题:Ballerina编译错误:annotation 'ballerina/http:2.4.5:ServiceConfig' is not allowed on var

我基于Ballerina Swan Lake Update 2(2201.2.3)版本编写了如下服务代码,包含MySQL客户端和运行在HTTPS监听器上的HTTP服务:

import ballerina/sql;
import ballerina/http;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;
import ballerina/log;

configurable int api_port = 8085;
configurable int api_port_sec = 9090;
configurable string host = ?;//"localhost";
configurable int port = ?;//33063;
configurable string user = ?;
configurable string password = ?;
configurable string database = ?;

public type Brewery record {|..|};

http:JwtValidatorConfig config = {
    issuer: "https://api.asgardeo.io/t/xxxx/oauth2/token",
    audience: "E2Bk9......................JpyJjwa",
    signatureConfig: {
        jwksConfig: {url: "https://api.asgardeo.io/t/xxx/oauth2/jwks"}
    }
};
 
listener http:Listener securedEP = new (api_port_sec,
    secureSocket = {
        key: {
            certFile: "./resources/public.crt",
            keyFile: "./resources/private.key"
        }
    }
);

@http:ServiceConfig {
    auth: [
        {
            jwtValidatorConfig: config
        }
    ]
}

final mysql:Client mysqlClient = check new (host = host, port = port, user = user, password = password, database = database);

service / on securedEP {
    function init() {
        log:printInfo("API started", host = "0.0.0.0", port = api_port_sec, protocol = "HTTPS");
    }

    # Description
    # Get Brewery by state
    # + state - Parameter Description
    # + return - Return Value Description
    isolated resource function get brewery/state/[string state]() returns Brewery[]|http:NotFound|error? {.....}

}

运行项目时出现编译错误:

ERROR [main.bal:(91:5,97:6)] annotation 'ballerina/http:2.4.5:ServiceConfig' is not allowed on var

解决方法

错误原因是@http:ServiceConfig注解被错误应用在mysqlClient变量上——该注解仅支持绑定到HTTP服务定义,不能用于普通变量。

只需将@http:ServiceConfig注解移动到HTTP服务定义的上方即可,修改后的代码如下:

import ballerina/sql;
import ballerina/http;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;
import ballerina/log;

configurable int api_port = 8085;
configurable int api_port_sec = 9090;
configurable string host = ?;//"localhost";
configurable int port = ?;//33063;
configurable string user = ?;
configurable string password = ?;
configurable string database = ?;

public type Brewery record {|..|};

http:JwtValidatorConfig config = {
    issuer: "https://api.asgardeo.io/t/xxxx/oauth2/token",
    audience: "E2Bk9......................JpyJjwa",
    signatureConfig: {
        jwksConfig: {url: "https://api.asgardeo.io/t/xxx/oauth2/jwks"}
    }
};
 
listener http:Listener securedEP = new (api_port_sec,
    secureSocket = {
        key: {
            certFile: "./resources/public.crt",
            keyFile: "./resources/private.key"
        }
    }
);

final mysql:Client mysqlClient = check new (host = host, port = port, user = user, password = password, database = database);

@http:ServiceConfig {
    auth: [
        {
            jwtValidatorConfig: config
        }
    ]
}
service / on securedEP {
    function init() {
        log:printInfo("API started", host = "0.0.0.0", port = api_port_sec, protocol = "HTTPS");
    }

    # Description
    # Get Brewery by state
    # + state - Parameter Description
    # + return - Return Value Description
    isolated resource function get brewery/state/[string state]() returns Brewery[]|http:NotFound|error? {.....}

}

调整后注解正确绑定到HTTP服务,编译错误即可消除。

内容的提问来源于stack exchange,提问作者Gayal Dassanayake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:16:39