import ballerina/http;

// Service declaration specifies base path for the resource names. The base path is `/` in this example.
service / on new http:Listener(8080) {
    // Resource method is associated with combination of accessor (`get`) and resource name (`hello`). 
    // Accessors are determined by the network protocol.
    // In HTTP resources, function parameters come from query parameters.
    resource function get hello(string name) returns string {
        return "Hello, " + name;
    }
}

Resource Methods

Service objects use remote methods to expose services in procedural style: remote methods are named by verbs.

Service objects use resource methods to expose services in an RESTful style: resources are named by nouns.

Resources are motivated by HTTP, but are general enough also to work for GraphQL. resource methods are a network-oriented generalization of OO getter/setter concept.

import ballerina/http;
service / on new http:Listener(8080) {

Service declaration specifies base path for the resource names. The base path is / in this example.

    resource function get hello(string name) returns string {
        return "Hello, " + name;
    }
}

Resource method is associated with combination of accessor (get) and resource name (hello). Accessors are determined by the network protocol. In HTTP resources, function parameters come from query parameters.

bal run resource_methods.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
# Run this cURL command to invoke the resource.
curl "localhost:8080/hello?name=Ballerina"
Hello, Ballerina