Annotations in Ballerina

What are Annotations?

An annotation is a ballerina code snippet that can be attached to some ballerina code, and will hold some metadeta related to that attached code. Annotations are not executable, but can be used to alter the behavior of some executable code.

service MyService {

Where Can I attach an Annotation?

Annotations can be attached to any one of the following:

  • Services
  • Resources
  • Functions
  • Connectors
  • Actions (of Connectors)
  • TypeMappers
  • Structs
  • Constants
  • Annotations

However, this differs for different types of annotations. For example, http:BasePath{} can only be attached to a Service, here as http:POST{} can only be attached to a resource. This is specified in the definition of each annotation.

What are the inbuilt ones?

Currently (in ballerina v0.85), there are inbuilt annotations in four packages, each are for HTTP, JMS, WebSockets, and Documentations. You can find the definitions of each annotations in [1], [2], [3], and [4] respectively.

Can I define my own Annotation?

Absolutely! You can define your own annotation and apply it anywhere you want. A definition of an annotation takes the following form:
annotation MyAnnotation attach service, resource, function {
    string status;
    int code;

Here the starting 'annotation' is a keyword that describes this is an annotation definition, followed by the name of your annotation. Then you can specify to where this annotation can be applied, after the keyword 'attach'. In the above sample, my 'MyAnnotation' is allowed to be attached to any service, resource, or a function (or all of them).

You can apply the above annotation in a function like below:
@bar:MyAnnotation{status:"working", code:1}
function myFunction(string str) {

Note that, if the function is in the same package as the annotation, then you need to ignore the preceding package name in the annotation attachment.
@MyAnnotation{status:"working", code:1}
function myFunction(string str) {

What can be the types of attributes?

Type of an attribute of an annotation can be on of the following:
  • A value type (string, int, float, boolean).
  • An Annotation.
  • Array of any of above.

Default Values for Annotation Attributes?

Indeed. Attributes of an annotation can have default values, But only for attributes of value types (string, int, float, boolean).

annotation MyAnnotation attach service, resource, function {
    string status = "failed";
    int code = 0;

This means, if you have an attribute of an array type (eg: int[] a), or an attribute of another annotation (eg MyAnnotation a;), then those attributes cannot have default values.

Annotate an Annotation by itself?

You can also annotate an annotation using the same annotation itself. For example, say we write an annotations called 'Desciption' to add an dexscription to any of the ballerina construct we write. And suppose we need to add a description to that 'Description' annotation. We can do it as follows:

@Description{value:"This is an annotation that can be used to describe a construct"}
annotation Description attach service, annotation, function {
    string value;

Will the above work? Absolutely!





  1. Microsoft office has a different version, and the entire version has the different setup file set up . Some of the version is Office 2013, Office 2016 and Office 365 etc. Go to . Norton is one of the most reliable antivirus providers in the market. Norton is delivering top rated protection from mobile devices and computers. Norton has 3 antivirus packages . McAfee have the complete set of features which can protect your digital online and offline life of the computing devices, and it not only help you to protect it install mcafee .If your PC is having a slow performance and you are facing problems due to this, then we can provide a solution to that. avira tech support . Our Avira Customer Support is open to those who have issues relating to downloading, updating or upgrading Avira Antivirus software avira support number .We can also assist you to confirm your product if you are not sure about its validity avira refund .

  2. Online Help – Step by Step guide for Norton Setup, Download & complete installation online. We are providing independent support service if in case you face problem to activate or Setup Norton product.

  3. Enter Key for, after purchasing Office from visit, sign in to your account then enter product key for Office Setup.

  4. Online Help :– Step by Step guide for McAfee Activate, Download & complete installation online. We are providing independent support service if in case you face problem to activate or Activate McAfee product. Just fill the form below and will get in touch with you as quick as possible.

  5. Online Help – Step by Step guide for office Setup, Download & complete installation online. We are providing independent support service if in case you face problem to activate or Setup office product.

  6. Looking for Aol Support? Call now on toll free number to get instant 24/7 email support AOL, AOL Mail Technical Support, Click here for more Email support

  7. Support For Adobe at is an online technical support company. We provide trustworthy tech support and services for the third party products.adobe support

  8. We provide Outlook customer service phone number or on Chat ,Outlook tech support phone number for recover outlook password recovery and other outlook support