import ballerina/http;
// The `absolute resource path` represents the absolute path to the service. When bound to a listener
// endpoint, the service will be accessible at the specified path. If the path is omitted, then it defaults to `/`.
// A string literal also can represent the absolute path. E.g., `"/foo"`.
// The `type descriptor` represents the respective type of the service. E.g., `http:Service`.
service http:Service /foo on new http:Listener(9090) {
// The `resource method name` (`post`) confines the resource to the specified HTTP methods. In this
// instance, only `POST` requests are allowed. The `default` accessor can be used to match with all methods
// including standard HTTP methods and custom methods.
// The `resource path` associates the relative path to the service object's path. E.g., `bar`.
resource function post bar(@http:Payload json payload) returns json {
return payload;
}
}
Service path and resource pathBallerina supports writing RESTful services according to the JAX-RS specification.
You can use the |
import ballerina/http;
service http:Service /foo on new http:Listener(9090) {
The absolute resource path
represents the absolute path to the service. When bound to a listener
endpoint, the service will be accessible at the specified path. If the path is omitted, then it defaults to /
.
A string literal also can represent the absolute path. E.g., "/foo"
.
The type descriptor
represents the respective type of the service. E.g., http:Service
.
resource function post bar(@http:Payload json payload) returns json {
return payload;
}
}
The resource method name
(post
) confines the resource to the specified HTTP methods. In this
instance, only POST
requests are allowed. The default
accessor can be used to match with all methods
including standard HTTP methods and custom methods.
The resource path
associates the relative path to the service object’s path. E.g., bar
.
bal run absolute_path_and_path.bal
Run the cURL command below to invoke the resource.
curl http://localhost:9090/foo/bar -d "{\"hello\": \"world\"}" -H "Content-Type: application/json"
{"hello":"world"}