JAX-RS - Injecting Application subclass with @Context annotation

[Updated: Sep 1, 2017, Created: Aug 31, 2017]

According to JAX-RS specification, the application created instance of an javax.ws.rs.core.Application subclass may be injected into resource classes and providers by using @Context annotation. The one of the advantages of injecting Application may be accessing the application-wide properties via Application#getProperties(). Let's see an example.

Example

The Application subclass

@ApplicationPath("/")
public class MyApp extends Application {

  @Override
  public Map<String, Object> getProperties() {
      Map<String, Object> map = new HashMap<>();
      map.put("KEY_MAX_ORDER_LIMIT", "100");
      return map;
  }
}

Injecting Application

@Path("/orders")
public class OrderResource {
  @GET
  public String getAllOrders(@Context Application app) {
      Map<String, Object> properties = app.getProperties();
      String props = properties.entrySet().stream()
                               .map(e -> e.getKey() + "=" + e.getValue())
                               .collect(Collectors.joining("\n"));
      System.out.println(props);
      return properties.get("KEY_MAX_ORDER_LIMIT").toString();
  }
}

The client

public class OrderClient {
  public static void main(String[] args) throws Exception {
      Client client = ClientBuilder.newBuilder().build();
      WebTarget target =
              client.target("http://localhost:8080/orders");
      Response response = target.request().get(Response.class);
      String responseString = response.readEntity(String.class);
      System.out.println("response: " + responseString);
  }
}

Output

response: 100

Output on server console

javax.servlet.context.tempdir=D:\LogicBig\example-projects\jax-rs\inject-application-via-context-annotation\target\tomcat\work\Tomcat\localhost\_
org.apache.catalina.resources=org.apache.naming.resources.ProxyDirContext@d6f2174
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.0"
         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>
    <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    ........
  </servlet>
 </web-app>
org.apache.tomcat.InstanceManager=org.apache.catalina.core.DefaultInstanceManager@2a6263c9
org.apache.catalina.jsp_classpath= .......
KEY_MAX_ORDER_LIMIT=100
org.apache.tomcat.JarScanner=org.apache.tomcat.util.scan.StandardJarScanner@1655865e

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

Asynchronous Processing Example Select All Download
  • inject-application-via-context-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also