Using Annotations

Contact Us or call 1-877-932-8228
Using Annotations

Using Annotations

To apply an annotation to a class or element, precede the item with the name of the annotation, prefixed with the @ symbol.

If the annotation takes parameters, supply them in parentheses, as a comma separated list of parameterName=parameterValue. If the only parameter is called value, then you can just supply the parameterValue, without specifying it by name.

@AnnotationName(value=parameterValue, parameter2Name=parameter2Value, ...)

Code Sample:

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import java.util.Date;

public class AnnotatedWebService {
  public Date getDate() {
    return new Date();

  public Date getFutureDate(@WebParam(name="days") int daysOffset) {
    Date d = new Date();
    d.setDate(d.getDate() + daysOffset);
    return d;

The @WebService annotation persists into compiled code. Tools that work with enterprise-level web servers (like Glassfish, JBoss, etc.), can read the annotation via reflection, and install the class as a web service, performing all the necessary tasks, such as creating a WSDL file, establishing a URL for the service, and installation it under that URL. The annotation can be parameterized with the name for the service (which defaults to the class name followed by "Service"), as well as several other items affecting the setup as a web service.

  • The serviceName parameter to the @WebService annotation tells the server to set up the service under that name, as opposed to the default, which would have been AnnotatedWebServiceService (the default behavior appends "Service" to the class name, which, in this case, would be redundant).
  • The @WebMethod annotations tell the server that these methods should be set up as RPC methods which can be remotely invoked (by default, the methods are exposed, the annotation is actually only needed when renaming the method, or to exclude the method from the service, but common practice is to include the unparameterized annotation for methods to be exposed as is).
  • The @WebParam annotation is used to rename the daysOffset parameter, which would be used from ordinary Java code, to days when the RPC is remotely invoked. Due to the limitations of reflection, where parameter names are not preserved into compiled classes, this annotation is necessary to avoid having the parameters be called arg0, arg1, etc.