I have a REST service implemented with JAX-RS. The web service is intended for testing purposes. My application has a HashMap
, which manages the objects I want to retrieve. How can I initialize this HashMap
when the service starts in order that the HashMap
has some objects I can retrieve? I tried to add some objects into the HashMap
in the constructor, but the HashMap
is empty when the service starts. I use the Jersey implementation of JAX-RS and configure my resources using the web.xml
file.
My web.xml
file has the following content:
<?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">
<display-name>OPMSimulator</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.ibm.opm.mobile.prototype.TestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
And my resource class has the following content:
public class Test {
private static HashMap<Integer, Database> databases;
@GET
@Produces(MediaType.TEXT_XML)
@Path("/database/{id}")
public String database(@PathParam("id")String id) {
Database database = databases.get(Integer.parseInt(id));
return XMLGenerator.getXML(database);
}
}
JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.
JAX-RS is a standard defined in Java Specification Request 311 (JSR-311) and Jersey / RESTEasy are implementations of it.
A JAX-RS application or implementation supplies a concrete subclass of this abstract class. The implementation-created instance of an Application subclass may be injected into resource classes and providers using Context . Constructor Summary.
In the constructor of your servlet should work (it's always called before calling doGet
and doPost
), but otherwise you can register a listener to initialize all your stuff:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class Manager implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
}
public void contextDestroyed(ServletContextEvent event) {
}
}
If you're not on Servlet 3.0
yet and can't upgrade, and thus can't use @WebListener
annotation, then you need to manually register it in /WEB-INF/web.xml
like below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
<display-name>projectName</display-name>
<listener>
<listener-class>Manager</listener-class>
</listener>
...
</web-app>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With