import ballerina/http;
import ballerina/io;type Student {
string Name;
int Grade;
map Marks;
};endpoint http:Listener helloEP {
port:9090
};@http:ServiceConfig
service<http:Service> hello bind helloEP {
@http:ResourceConfig {
methods:["POST"],
body:"orderDetails"
}
bindJson (endpoint outboundEP, http:Request req, json orderDetails) {
json details = orderDetails.Details;
io:println(details); http:Response res = new;
res.setJsonPayload(details);
_ = outboundEP -> respond(res);
}
@http:ResourceConfig {
methods:["POST"],
body:"store",
consumes:["application/xml"]
}
bindXML (endpoint outboundEP, http:Request req, xml store) {
xml city = store.selectChildren("city");
io:println(city); http:Response res = new;
res.setXmlPayload(city);
_ = outboundEP -> respond(res);
}
@http:ResourceConfig {
methods:["POST"],
body:"student",
consumes:["application/json"]
}
bindStruct (endpoint outboundEP, http:Request req, Student student) {
string name = student.Name;
io:println(name); int grade = student.Grade;
io:println(grade); map marks = student.Marks;
io:println(marks); http:Response res = new;
res.setJsonPayload({Name:name, Grade:grade});
_ = outboundEP -> respond(res);
}
}
HTTP Data BindingHTTP data binding helps accessing payload through the last resource signature parameter. Parameter name should be declared in resource config under "body" annotation and String, JSON, XML, BLOB and Struct are supported as parameter types. |
|
import ballerina/http;
import ballerina/io;
|
|
type Student {
string Name;
int Grade;
map Marks;
};
|
|
endpoint http:Listener helloEP {
port:9090
};
|
|
@http:ServiceConfig
service<http:Service> hello bind helloEP {
|
|
@http:ResourceConfig {
methods:["POST"],
body:"orderDetails"
}
bindJson (endpoint outboundEP, http:Request req, json orderDetails) {
|
Body annotation represents the entity body of the inbound request. |
json details = orderDetails.Details;
io:println(details);
|
Access JSON field values |
http:Response res = new;
res.setJsonPayload(details);
_ = outboundEP -> respond(res);
}
|
|
@http:ResourceConfig {
methods:["POST"],
body:"store",
consumes:["application/xml"]
}
bindXML (endpoint outboundEP, http:Request req, xml store) {
|
Bind xml payload of the inbound request to variable store. |
xml city = store.selectChildren("city");
io:println(city);
|
Access XML content. |
http:Response res = new;
res.setXmlPayload(city);
_ = outboundEP -> respond(res);
}
|
|
@http:ResourceConfig {
methods:["POST"],
body:"student",
consumes:["application/json"]
}
bindStruct (endpoint outboundEP, http:Request req, Student student) {
|
Bind JSON payload into a custom struct. Payload content should match with struct. |
string name = student.Name;
io:println(name);
|
Access Student struct fields |
int grade = student.Grade;
io:println(grade);
|
|
map marks = student.Marks;
io:println(marks);
|
|
http:Response res = new;
res.setJsonPayload({Name:name, Grade:grade});
_ = outboundEP -> respond(res);
}
}
|
|
$ ballerina run http-data-binding.bal
|
To run the service, put the code in |
ballerina: initiating service(s) in 'http-data-binding.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090
|
Service deployment: |
$ curl -v http://localhost:9090/hello/bindJson -X POST -d '{ "Details": { "ID": "77999", "Name": "XYZ"} , "Location": { "No": "01", "City": "Colombo"}}' -H "Content-Type:application/json"
|
To invoke the bindJson resource, use following client. |
{"ID":"77999","Name":"XYZ"}
|
Server response: |
$ curl -v http://localhost:9090/hello/bindXML -X POST -d "<h:Store id = \"AST\" xmlns:h=\"http://www.test.com\"><h:street>Main</h:street><h:city>94</h:city></h:Store>" -H "Content-Type:application/xml"
|
To invoke the bindXML resource, use following client. |
<h:city xmlns:h="http://www.test.com">94</h:city>
|
Server response: |
$ curl -v http://localhost:9090/hello/bindStruct -X POST -d '{ "Name": "John", "Grade": 12, "Marks": {"English" : "85", "IT" : "100"}}' -H "Content-Type:application/json"
|
To invoke the bindStruct resource, use following client. |
{"Name":"John","Grade":12}
|
Server response: |