import ballerina/http;
import ballerina/mime;
endpoint http:Listener infoServiceEP {
    port:9092
};
@http:ServiceConfig {basePath:"infoService"}
service<http:Service> infoService bind infoServiceEP {    @http:ResourceConfig {
        methods:["POST"],
        path:"/",
        consumes:["text/json", "application/json"],
        produces:["application/xml"]
    }
    student (endpoint conn, http:Request req) {
        http:Response res = new;
        var msg = req.getJsonPayload();
        match msg {
            json jsonMsg => {
                string nameString = check <string>jsonMsg["name"];
                string payload = "<name>" + nameString + "</name>";
                xml name = check <xml>payload;
                res.setXmlPayload(name);
            }
            http:PayloadError payloadError => {
                res.statusCode = 500;
                res.setStringPayload(payloadError.message);            }
        }
        _ = conn -> respond(res);
    }
}

Produces/Consumes

The sample explains the behaviour of the Consumes and Produces annotation attributes of resourceConfig in ballerina.

import ballerina/http;
import ballerina/mime;
endpoint http:Listener infoServiceEP {
    port:9092
};
@http:ServiceConfig {basePath:"infoService"}
service<http:Service> infoService bind infoServiceEP {

Consumes and Produces annotations contain MIME types as an array of strings.

    @http:ResourceConfig {
        methods:["POST"],
        path:"/",
        consumes:["text/json", "application/json"],
        produces:["application/xml"]
    }
    student (endpoint conn, http:Request req) {

Resource can consume/accept text/json and application/json media types only. Therefore Content-Type header must have one of the types. Resource can produce application/xml payloads. Therefore Accept header should be set accordingly.

        http:Response res = new;
        var msg = req.getJsonPayload();
        match msg {
            json jsonMsg => {

Get JSON payload from the request message.

                string nameString = check <string>jsonMsg["name"];

Get the string value relevant to the key “name”.

                string payload = "<name>" + nameString + "</name>";
                xml name = check <xml>payload;
                res.setXmlPayload(name);
            }
            http:PayloadError payloadError => {
                res.statusCode = 500;
                res.setStringPayload(payloadError.message);

Create XML payload and respond back.

            }
        }
        _ = conn -> respond(res);
    }
}
$ ballerina run produces-consumes.bal

To run the service, put the code in produces-consumes.bal and use $BALLERINA_HOME/bin.

ballerina: initiating service(s) in 'produces-consumes.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090

Service deployment:

$ curl -v http://localhost:9090/infoService -H "Accept:application/xml" -H "Content-Type:application/json" -d '{"name":"Ballerina"}'

To invoke the service, use following client.

<name>Ballerina</name>

Server response: