Redirect 如何从芭蕾舞演员Lang处进行重定向?

Redirect 如何从芭蕾舞演员Lang处进行重定向?,redirect,ballerina,Redirect,Ballerina,我在ballerinalang中实现了一个名为https://localhost:9090/isValidUser。下面是我的代码 import ballerina.net.http; @http:configuration { basePath:"/", httpsPort:9090, keyStoreFile:"${ballerina.home}/bre/security/wso2carbon.jks", keyStorePassword:"wso2carb

我在ballerinalang中实现了一个名为
https://localhost:9090/isValidUser
。下面是我的代码

import ballerina.net.http;

@http:configuration {
    basePath:"/",
    httpsPort:9090,
    keyStoreFile:"${ballerina.home}/bre/security/wso2carbon.jks",
    keyStorePassword:"wso2carbon",
    certPassword:"wso2carbon",
    trustStoreFile:"${ballerina.home}/bre/security/client-truststore.jks",
    trustStorePassword:"wso2carbon"
}
service<http> authentication {
    @http:resourceConfig {
        methods:["POST"],
        path:"/isValidUser"
    }

    resource isValidUser (http:Request req, http:Response res) {
        println(req.getHeaders());
        res.send();

    }
}
import ballerina.net.http;
@http:配置{
基本路径:“/”,
httpsPort:9090,
keystrefile:“${ballerina.home}/bre/security/wso2carbon.jks”,
密钥密码:“wso2carbon”,
证书密码:“wso2carbon”,
trustStoreFile:“${ballerina.home}/bre/security/client truststore.jks”,
trustStorePassword:“wso2carbon”
}
服务认证{
@http:resourceConfig{
方法:[“发布”],
路径:“/isValidUser”
}
资源isValidUser(http:Request-req,http:Response-res){
println(req.getHeaders());
res.send();
}
}
现在我需要做的是,当我从浏览器调用该URL时,我需要将用户重定向到另一个名为
https://localhost:3000
在我的服务中进行一些验证之后


那么,我如何从芭蕾舞演员Lang那里进行重定向呢

在Ballerina中,您需要通过设置必要的标题和状态代码来处理重定向。下面的示例是一个简单的演示,演示了如何在Ballerina中重定向。(注:我曾在《芭蕾舞演员》0.95.2中尝试过这一点)

import ballerina.net.http;
@http:configuration{basePath:“/hello”}
服务helloWorld{
@http:resourceConfig{
方法:[“获取”],
路径:“/”
}
资源sayHello(http:Request-Request,http:Response-Response){
map qParams=request.getQueryParams();
变量名称,=(字符串)qParams.name;
如果(isExistingUser(名称)){
response.setStringPayload(“你好,芭蕾舞女演员!”);
}否则{
response.setHeader(“位置”http://localhost:9090/hello/newUser");
响应。设置状态代码(302);
}
_=response.send();
}
@http:resourceConfig{path:“/newUser”}
资源newUser(http:Request-Request,http:Response-Response){
string msg=“芭蕾舞演员新手?欢迎!”;
响应。setStringPayload(msg);
_=response.send();
}
}
函数isExistingUser(字符串名称)(布尔值){
返回名称==“芭蕾舞演员”;
}

Ballerina提供了平滑的API来执行重定向。请检查以下详细说明侦听器端点重定向的代码

service<http:Service> redirect1 bind {port:9090} {
    @http:ResourceConfig {
        methods:["GET"],
        path:"/"
    }
    redirect1 (endpoint client, http:Request req) {
        http:Response res = new;
        _ = client -> redirect(res, http:REDIRECT_TEMPORARY_REDIRECT_307,
            ["http://localhost:9093/redirect2"]);
    }
}
服务重定向1绑定{port:9090}{
@http:ResourceConfig{
方法:[“获取”],
路径:“/”
}
重定向1(端点客户端,http:Request-req){
http:Response res=new;
_=客户端->重定向(res,http:redirect\u TEMPORARY\u redirect\u 307,
["http://localhost:9093/redirect2"]);
}
}
完整的示例在中提供

service<http:Service> redirect1 bind {port:9090} {
    @http:ResourceConfig {
        methods:["GET"],
        path:"/"
    }
    redirect1 (endpoint client, http:Request req) {
        http:Response res = new;
        _ = client -> redirect(res, http:REDIRECT_TEMPORARY_REDIRECT_307,
            ["http://localhost:9093/redirect2"]);
    }
}