我目前正在开发一个REST Web服务,该服务使用过滤器来处理身份验证(JWT令牌)。筛选器工作正常,但我无法将对象返回资源(该对象包含有关域访问和管理员ID的信息)。 环境是在WSO2应用程序服务器版本5.3.0上运行的CXF,Jersey,Jackson(JSON REST)
我一直在使用ContainerRequestContext
方法,但是每次尝试访问资源代码中的上下文时,我都会得到一个NullPointerException
。我做了一个简单的测试用例Web服务,专门尝试返回对象,但这在资源代码中也得到了NullPointerException
。请记住,ContainerRequestContext
在过滤器代码中确实有效;我之前通过控制台调试对此进行了测试。
有关我现在正在使用的代码,请参见下文。 感谢所有帮助。
ContextTest.java
package test.codehq.fab1.contexttest.cxf3test1;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;
ContainerRequestContext containerRequestContext;
public ContextTest()
{
}
@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");
Map<String, String> myResponse = new HashMap<String, String>();
containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);
if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");
GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}
AuthorizationNeededInterface.java
package test.codehq.fab1.contexttest.cxf3test1;
import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{
}
AuthorizationNeeded.java
package test.codehq.fab1.contexttest.cxf3test1;
import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;
@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}
cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>
<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>
<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
</jaxrs:server>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>
在对“ echos”资源进行请求时,出现以下异常:
java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor
org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider.<init>(PerRequestResourceProvider.java:57)
org.apache.cxf.jaxrs.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.cxf.jaxrs.impl.tl.ThreadLocalInvocationHandler.invoke(ThreadLocalInvocationHandler.java:48)
com.sun.proxy.$Proxy1694.getResource(Unknown Source)
test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded.filter(AuthorizationNeeded.java:23)
org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1688)
答案 0 :(得分:0)
UPDATE 我已经解决了这个问题。这个问题仍然无法解决,因此请记住这一点。我也张贴了 有效的CXF和Jersey版本。请参阅下面的版本 虚线。我认为在CXF中,ContainerRequestContext在资源类中不可用。我不知道为什么泽西岛使这一切成为可能,但这看起来就像他们实现它的方式一样。我通过查看lefloh的示例How to Pass Object from ContainerRequestFilter to Resource找到了解决方案 谢谢lefloh指出了ContainerRequestFilter属性与HttpServletRequest同步的事实。
CXF版本(有效)
ContextTest.java
package test.codehq.fab1.contexttest.cxf3test1;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.ManagedBean;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@ManagedBean
@Path("/")
@Produces("application/json")
public class ContextTest
{
// Keep in mind that the ContainerRequestContext
// attributes are automatically synced with
// HttpServletRequest. When using CXF the
// ContainerRequestContext can not be directly
// accessed in the Resource, so we need to use
// HttpServletRequest
@Context
HttpServletRequest httpServletRequest;
public ContextTest()
{
}
@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");
Map<String, String> myResponse = new HashMap<String, String>();
if(httpServletRequest != null)
{
if(httpServletRequest.getAttribute("AuthID") != null)
{
if(((String)httpServletRequest.getAttribute("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");
GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}
AuthorizationNeededInterface.java
package test.codehq.fab1.contexttest.cxf3test1;
import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{
}
AuthorizationNeeded.java
package test.codehq.fab1.contexttest.cxf3test1;
import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
public AuthorizationNeeded()
{
}
@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
conRequestContext.setProperty("AuthID", "1234");
}
}
cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>
<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="ContextTestBean"/>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>
</jaxrs:server>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-compatible</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>
Jersey版本(有效)
ContextTest.java
package test.codehq.fab1.contexttest.jersey2test1;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;
ContainerRequestContext containerRequestContext;
public ContextTest()
{
}
@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");
Map<String, String> myResponse = new HashMap<String, String>();
containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);
if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");
GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}
AuthorizationNeededInterface.java
package test.codehq.fab1.contexttest.jersey2test1;
import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{
}
AuthorizationNeeded.java
package test.codehq.fab1.contexttest.jersey2test1;
import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;
@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestJersey2test1</display-name>
<servlet>
<description>Jersey Filter test</description>
<display-name>contexttest-jersey2test1</display-name>
<servlet-name>contexttest-jersey2test1</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test.codehq.fab1.contexttest.jersey2test1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>contexttest-jersey2test1</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestJersey2Test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json</artifactId>
<version>2.0-m05-1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
</dependencies>
</project>