2017-11-22 1 views
0

ballerinalang에 https://localhost:9090/isValidUser이라는 REST 엔드 포인트를 구현했습니다. 그리고 여기에 지금은 할 필요가ballerinalang에서이 리디렉션을 어떻게 수행합니까?

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(); 

    } 
} 

아래에있는 내 코드는 내가 브라우저에서 해당 URL을 호출 할 때, 나는 어떤 검증 나의 서비스 내에서 발생 후 https://localhost:3000라는 또 다른 URL로 사용자를 리디렉션 할 필요가이다.

이렇게 어떻게 발레리 날란에서이 방향 전환을 할 수 있습니까?

답변

0

발레리나에서는 필요한 헤더와 상태 코드를 설정하여 리디렉션을 직접 처리해야합니다. 다음 예는 발레리나에서 리디렉션하는 방법에 대한 간단한 데모입니다. (참고 : 나는 Ballerina 0.95.2에서 이것을 시도했다.)

import ballerina.net.http; 

@http:configuration {basePath:"/hello"} 
service<http> helloWorld { 

    @http:resourceConfig { 
     methods:["GET"], 
     path:"/" 
    } 
    resource sayHello (http:Request request, http:Response response) { 
     map qParams = request.getQueryParams(); 
     var name, _ = (string)qParams.name; 

     if (isExistingUser(name)) { 
      response.setStringPayload("Hello Ballerina!"); 
     } else { 
      response.setHeader("Location", "http://localhost:9090/hello/newUser"); 
      response.setStatusCode(302); 
     } 

     _ = response.send(); 
    } 

    @http:resourceConfig {path:"/newUser"} 
    resource newUser (http:Request request, http:Response response) { 
     string msg = "New to Ballerina? Welcome!"; 
     response.setStringPayload(msg); 
     _ = response.send(); 
    } 
} 

function isExistingUser (string name) (boolean) { 
    return name == "Ballerina"; 
} 
관련 문제