Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization of JAX-RS application

Tags:

java

rest

jax-rs

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);
    }
}
like image 498
Javiator Avatar asked Oct 19 '12 07:10

Javiator


People also ask

What is a JAX-RS application?

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.

Which of these are implementations of JAX-RS API?

JAX-RS is a standard defined in Java Specification Request 311 (JSR-311) and Jersey / RESTEasy are implementations of it.

What is javax WS RS core application?

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.


1 Answers

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>
like image 134
lapo Avatar answered Oct 14 '22 09:10

lapo