Difference Between ResourceConfig and ServletContextListener for Jersey Recreation

I want to initialize the Jersey Rest service and enter a global application variable that should be calculated when the application starts, and should be available in each rest resource and each method (here is an integer globalAppValue = 17, but it will be a complex object later).

To initialize the service and calculate the value once at startup, I found two methods: the generic ServletContextListener method and the ResourceConfig method in Jersey. But I did not understand what is the difference between them? Both methods start at startup (both System.out messages are printed).

Here is an implementation of my ServletContextListener that works fine:

public class LoadConfigurationListener implements ServletContextListener
{
    private int globalAppValue = 17;

    @Override
    public void contextDestroyed (ServletContextEvent event)
    {
    }

    @Override
    public void contextInitialized (ServletContextEvent event)
    {
        System.out.println ("ServletContext init.");

        ServletContext context = event.getServletContext ();
        context.setAttribute ("globalAppValue", globalAppValue);
    }
}

Jersey Rest ResourceConfig, ServletContext . - :

@ApplicationPath("Resources")
public class MyApplication extends ResourceConfig
{
    @Context
    ServletContext context;

    private int globalAppValue = 17;

    public MyApplication () throws NamingException
    {
        System.out.println ("Application init.");

        // returns NullPointerException since ServletContext is not injected
        context.setAttribute ("globalAppValue", 17);
    }

    public int getAppValue ()
    {
        return globalAppValue;
    }
}

:

@Path("/")
public class TestResource
{
    @Context
    ServletContext context;
    @Context
    MyApplication application;

    @Path("/test")
    @GET
    public String sayHello () throws SQLException
    {
        String result = "Hello World: ";

        // returns NullPointerException since application is not injected
        result += "globalAppValue=" + application.getAppValue ();

        // works!
        result += "contextValue=" + context.getAttribute ("globalAppValue");

        return result;
    }
}

, ServletContextListener , ResourceConfig/Application, , , , . , . !

+4
2

ResourceConfig, property( key, value ).

public MyApplication() {
    property("MyProp", "MyValue");
}

javax.ws.rs.core.Application, ResourceConfig .

, , API Application . , , getProperties(), .

@Path("/")
public class TestResource
{
    @Context
    Application application;

    @GET
    public String get() {
        String value = (String)application.getProperties().get("MyProp");
    }
}

, property ResourceConfig, javax.ws.rs.core.Configuration, . Application Configuration

@Path("/")
public class TestResource
{
    @Context
    Configuration config;

    @GET
    public String get() {
        String value = (String)config.getProperty("MyProp");
    }
}

. :

+4

no sory, "value = null", GET/test/.

package rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;

@Path("/test")
public class TestResource
{
    @Context
    Application application;

    @GET
    public String sayHello () 
    {
        String result = "value=" + application.getProperties ().get ("value");

        return result;
    }
}

ApplicationPath ""?

package rest;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("resources")
public class MyApplication extends ResourceConfig
{
    public MyApplication ()
    {
        property ("value", 17);
        System.out.println (getProperties ());
    }
}

EDIT: , , / . web.xml,

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

* url- <url-pattern>/</url-pattern> ( *) , ,

@ApplicationPath("/")
public class MyApplication extends ResourceConfig

- . , ApplicationPath , Servlet-Url web.xml, .

0

Source: https://habr.com/ru/post/1608285/


All Articles