Close

JAX-RS - Configuration

[Last Updated: Nov 30, 2017]

Configuration can be used to access components and the meta-data for the configured context. Following is the declaration of this interface:

package javax.ws.rs.core;
....
public interface Configuration {
    /**
      * Returns configuration context runtime type enum.
      * either RuntimeType.CLIENT or RuntimeType.SERVER
      */
    public RuntimeType getRuntimeType();

    /**
      * Returns the immutable view of configuration properties.
      * This includes the properties defined by the current Application.
      */
    public Map<String, Object> getProperties();

    /**
      * Returns the property value for the specified property name.
      */
    public Object getProperty(String name);

    /**
      * Returns Collection of all property names
      */
    public Collection<String> getPropertyNames();

    /**
      * Returns true if the feature instance has been previously enabled in this
      * configuration context, false otherwise.
      */
    public boolean isEnabled(Feature feature);

    /**
      * Returns true if a feature of a given class has been previously enabled in this
      * configuration context, false otherwise.
      */
    public boolean isEnabled(Class<? extends Feature> featureClass);

    /**
      * Returns true if the component instance has been previously registered in this
      * configuration context, false otherwise.
      */
    public boolean isRegistered(Object component);

    /**
    * Returns true if a component of a given class has been previously registered in this
    * configuration context, false otherwise.
    */
    public boolean isRegistered(Class<?> componentClass);

    /**
      * Returns map of extension contracts and their priorities for which the component class
      * is registered.
      */
    public Map<Class<?>, Integer> getContracts(Class<?> componentClass);

    /**
      * Returns the immutable set of registered JAX-RS component classes.
      */
    public Set<Class<?>> getClasses();

    /**
    * Returns the immutable set of registered JAX-RS component instances.
    */
    public Set<Object> getInstances();
}

The instance of this interface can be injected with @Context annotation.

In the following example, we will see how to access current context properties. We will be covering others in future tutorials.

Example

@Path("/")
public class ExampleResource {
  @Context
  private Configuration configuration;

  @GET
  @Path("/test1")
  public String getResponse() {
      Map<String, Object> properties = configuration.getProperties();
      properties.forEach((k, v) -> System.out.println(k + " = " + v));
      return "test response";
  }
}
@ApplicationPath("/")
public class MyApp extends Application {
  @Override
  public Map<String, Object> getProperties() {
      Map<String, Object> map = new HashMap<>();
      map.put("TEST_PROP", "100");
      return map;
  }
}

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

The client

public class ExampleClient {
  public static void main(String[] args) {
      Client client = ClientBuilder.newClient();
      WebTarget target =
              client.target("http://localhost:8080/test1");
      Response response = target.request()
                                .get();
      System.out.printf("status: %s%n", response.getStatus());
      System.out.printf("-- response body --%n%s%n", response.readEntity(String.class));
  }
}

Output

status: 200
-- response body --
test response

Output On Server console

javax.servlet.context.tempdir = D:\LogicBig\example-projects\jax-rs\context-configuration-example\target\tomcat\work\Tomcat\localhost\_
org.apache.catalina.resources = org.apache.naming.resources.ProxyDirContext@4beddcf7
TEST_PROP = 100
org.apache.tomcat.util.scan.MergedWebXml = <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.1"
             metadata-complete="true">
  <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
      <param-name>listings</param-name>
      <param-value>false</param-value>
    </init-param>
    ....
</web-app>
org.apache.tomcat.InstanceManager = org.apache.catalina.core.DefaultInstanceManager@3140b355
org.apache.catalina.jsp_classpath = /D:/jax-rs/context-configuration-example/target/context-configuration-example......
javax.websocket.server.ServerContainer = org.apache.tomcat.websocket.server.WsServerContainer@2839eb58
org.apache.tomcat.JarScanner = org.apache.tomcat.util.scan.StandardJarScanner@1cbf3ccf

Example Project

Dependencies and Technologies Used:

  • jersey-server 2.25.1: Jersey core server implementation.
  • jersey-container-servlet 2.25.1: Jersey core Servlet 3.x implementation.
  • JDK 1.8
  • Maven 3.3.9

'@Context Configuration' Example Select All Download
  • context-configuration-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleResource.java

    See Also