import ballerina/http;
import ballerina/log;
service hello on new http:Listener(9090) {
    resource function sayHello(http:Caller caller, http:Request req) {
        var result = caller->respond("Hello, World!");
        if (result is error) {
            log:printError("Error sending response", result);
        }
    }
}

Hello World Service

In Ballerina, services represent collections of network-accessible entry points. Resources represent one such entry point. How a resource is exposed over a network protocol depends on the listener to which a service is attached.

import ballerina/http;
import ballerina/log;
service hello on new http:Listener(9090) {

By default, Ballerina exposes an HTTP service via HTTP/1.1.

    resource function sayHello(http:Caller caller, http:Request req) {

Resource functions are invoked with the HTTP caller and the incoming request as arguments.

        var result = caller->respond("Hello, World!");

Send a response back to the caller.

        if (result is error) {
            log:printError("Error sending response", result);
        }
    }
}

Log the error in case of a failure.

# To start the service, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run hello_world_service.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
$ curl http://localhost:9090/hello/sayHello
Hello, World!

Invoke the service using this “curl” command.