import ballerina/http;

type Args record {|
    decimal x;
    decimal y;
|};

listener http:Listener h = new (9090);

service /calc on h {
    // Resource method arguments can use user-defined types.
    // Annotations can be used to refine the mapping between 
    // Ballerina-declared type and wire format.
    resource function post add(@http:Payload Args args) 
            returns decimal {
        return args.x + args.y;
    }
}

Resource Method Typing

Resource method arguments can use user-defined types. Listener will use introspection to map from protocol format (typically JSON) to user-defined type, using cloneWithType. Return value that is subtype of anydata will be mapped from user-defined type to protocol format, typically JSON, using toJson. Can generate API description (e.g. OpenAPI) from Ballerina service declaration. Annotations can be used to refine the mapping between Ballerina-declared type and wire format.

import ballerina/http;
type Args record {|
    decimal x;
    decimal y;
|};
listener http:Listener h = new (9090);
service /calc on h {
    resource function post add(@http:Payload Args args) 
            returns decimal {
        return args.x + args.y;
    }
}

Resource method arguments can use user-defined types. Annotations can be used to refine the mapping between Ballerina-declared type and wire format.

bal run resource_method_typing.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
curl http://localhost:9090/calc/add -d "{\"x\": 1.0, \"y\": 2.0}"
3.0