Properties for a web application
The properties file needs to reside anywhere on the classpath (use src/main/java)
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLookup {
Properties prop = new Properties();
String filename = "config.properties";
InputStream input = null;
public PropertiesLookup() {
try {
input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return;
}
prop.load(input);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return prop.getProperty(key);
}
}
and a sample service to test:
@GET
@Path("properties/{property}")
@Produces(MediaType.APPLICATION_JSON)
public Response propertiesCheck(@PathParam("property") String property) {
PropertiesLookup pl = new PropertiesLookup();
String pValue = pl.getProperty(property);
return Response.status(200).entity(pValue).build();
}