ОБНОВЛЕНИЕ Я решил вопрос.Код, который не работает, все еще находится в этом вопросе, так что имейте это в виду.Я также разместил код версий CXF и Jersey, которые работают.Смотрите эти версии под пунктирной линией.Я думаю, что в CXF ContainerRequestContext не доступен в классе ресурсов.Я не знаю, почему Джерси делает это возможным, но это похоже на то, как они это реализовали.Я нашел решение, посмотрев на пример из lefloh, Как передать объект из ContainerRequestFilter в 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>
Версия на Джерси (рабочая)
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>