Swan Lake Beta3 is here.
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
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
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand