Load Properties File from WEB-INF in JAX-RS Jersey App

Often we need to load up and read a value from a property file which is stored in a WEB-INF folder of our web app. In the below example I am going to share with you how to create a web service which accepts a key, reads key value from a property file and returns back a value corresponding to that key as a plain text response. In a little more details we are going to cover the following:

  • Create a very simple JAX-RS Web App using Maven
  • Add needed dependencies into pom.xml file
  • Create a Root Resource class which will accept HTTP GET request with the Query Param
  • Load up a property file from WEB-INF folder
  • Read a value from a property file which corresponds to the key which we have received via HTTP GET request as a URL Query Parameter
  • Return key value we have read from a property file as a plain text response.

Create JAX-RS Web App with Maven

To create a new JAX-RS Web App with Maven please follow this short example I have prepared a bit earlier: Create a Jersey JAX-RS project with Maven

Add Dependencies to POM.XML file

For us to be able to access Servlet Context and eventually read the property file which is stored under WEB-INF we need to add the support for javax.servlet-api to our pom.xml file.

Here is how my complete pom.xml file looks like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.appsdeveloperblog.ws</groupId>
    <artifactId>CodeExamplesWebApp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>CodeExamplesWebApp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -->
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.25</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>CodeExamplesWebApp</finalName>
    </build>
</project>

Create Web Service Endpoint 

With the below code example we will create a new Root Resource class which will have one java method which accepts HTTP GET request and reads one URL Query Parameter with a name “key”.  The code that loads up properly file will be added in the next code snippet.

package com.appsdeveloperblog.ws.entrypoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
 
@Path("/users")
public class UsersEntryPoint {

    @GET
    @Produces({MediaType.TEXT_PLAIN})
    public String getCurrency(@QueryParam("key") String key) {
 
        String returnValue = null;
        
        if( key == null || key.isEmpty() ) return returnValue;
        
        return returnValue;

    }

}

Load Properties File from WEB-INF folder

To be able to read a properties file stored under WEB-INF folder we will need to access ServletContext. We will do it by injecting the ServletContext into the Root Resource Class with the @Context annotation. Once we have the ServletContext available to us, we can get the RealPath to the properties file and read it’s content.

Root Resource Class Complete Code Example

package com.appsdeveloperblog.ws.entrypoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
 
@Path("/users")
public class UsersEntryPoint {

    @Context
    private ServletContext context;

    @GET
    @Produces({MediaType.TEXT_PLAIN})
    public String getCurrency(@QueryParam("key") String key) {
 
        String returnValue = null;
        
        if( key == null || key.isEmpty() ) return returnValue;
        
        String realPath = context.getRealPath("/WEB-INF/regional.properties");

        try {
            Properties props = new Properties();
            props.load(new FileInputStream(new File(realPath)));

            returnValue = props.getProperty(key);
 
        } catch (IOException ex) {
            Logger.getLogger(UsersEntryPoint.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return returnValue;

    }

}

I hope this short blog post is helpful to you. If you would like to learn more about Building RESTful Web Services with Java checkout some of the best video courses below or checkout my resources page which has links to video courses and tutorials on different subjects like Maven, Git, Amazon Cloud, Android, Swift and everything you need to know to become a full stack mobile app developer.

Enjoy!

Build RESTful Web Services with Java. Video Courses.

Java Web Services Part 1. Video Course.

Java icon

Java Web Services Part 2. Video Course.

Master advanced web services concepts and implement them in easy steps Java Web Services Part 2 icon

REST Java Web Services. Video Course.

A guide to understanding, accessing, and writing a REST Java web service using Apache and Java EE. Java Web Services Part 2 icon

Leave a Reply

Your email address will not be published. Required fields are marked *