import ballerina/http;
endpoint http:Listener helloWorldEP {
port:9090
};
service<http:Service> hello bind helloWorldEP {
sayHello (endpoint conn, http:Request req) {
http:Response res = new;
res.setStringPayload("Hello, World!");
_ = conn -> respond(res);
}
}
Hello World ServiceServices represent collections of network accessible entry points in Ballerina. Resources represent one such entry point. Exactly how the resource is exposed over a network protocol is dependent on the server connector in use for the service as well as on annotations that are given for the specific resource. |
|
import ballerina/http;
|
|
endpoint http:Listener helloWorldEP {
port:9090
};
|
Attributes associated with the service endpoint is defined here. |
service<http:Service> hello bind helloWorldEP {
|
By default Ballerina assumes that the service is to be exposed via HTTP/1.1. |
sayHello (endpoint conn, http:Request req) {
http:Response res = new;
|
All resources are invoked with arguments of server connector and request |
res.setStringPayload("Hello, World!");
|
A util method that can be used to set string payload. |
_ = conn -> respond(res);
}
}
|
Sends the response back to the client. |
$ ballerina run hello-world-service.bal
ballerina: initiating service(s) in 'hello-world-service.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090
|
To start the service, put the code in “hello-world-service.bal” and use “ballerina run” command. |
$ ballerina build hello-world-service.bal
$ ls
hello-world-service.balx hello-world-service.bal
|
To build a compiled program file, we can use the “ballerina build” command followed by the service package. |
$ curl http://localhost:9090/hello/sayHello
Hello, World!
|
Invoke the service using “curl”. |