uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

17.1.11 Inheriting annotations

The child classes or implementation classes inherit the annotations of the JAX-RS specifications used in the methods of an interface or a parent class.

The conditions for inheriting the annotations are as follows:

When a parent class is inherited and an interface has been implemented, if both of the above conditions for inheriting annotations are fulfilled, the annotations of the parent class take precedence.

When multiple parent classes are inherited and multiple interfaces have been implemented, the annotation of the parent class that was inherited first or that of the interface that was implemented first should get precedence.

An example of inheriting annotations is as follows.

package com.sample.resources;
 
import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
 
//interface
public interface A {
 
  //method that uses the annotation of JAX-RS specifications
  @GET
  public String getValue(@QueryParam("query") String query);
 
}
 
package com.sample.resources;
 
import javax.ws.rs.Path;
 
//root resource class that implements the interface
@Path("/root/")
public class Resource implements A {
 
  //implementation of method
  public String getValue(String query) {
    //implementation
 
  }
}
 
package com.sample.resources;
 
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
 
//root resource class that implements the interface
@Path("/root1/")
public class Resource1 implements A {
 
  //implementation of method
  @Produces("text/xml")
  public String getValue(String query) {
    //implementation
  }
}

In this example, consider the context root of the Web application (WAR file) containing the interface com.sample.resources.A and the root resource classes com.sample.resources.Resource and com.sample.resources.Resource1 to be "resource" and that the Web application is published on a host named "example.com".

The HTTP GET request corresponding to the URL "http://example.com/resource/root?query=10" is dispatched to the resource method getValue(). This is because the resource method getValue() inherits the GET annotation of the interface.

However, the HTTP GET request corresponding to the URL "http://example.com/resource/root1?query=10" is not dispatched to the resource method getValue() and the system returns 500 as the HTTP status code. This is because the resource method getValue() does not inherit the GET annotation of the interface.