I have a REST service implemented with JAX-RS. The web service is intended for testing. My application has a HashMap that manages the objects I want to receive. How can I initialize this HashMap when the service starts, so that the HashMap some objects that I can get? I tried adding some objects to the HashMap in the constructor, but the HashMap empty when the service starts. I use the JAX-RS implementation in Jersey and set up my resources using the web.xml .
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); } }
Javiator
source share