user1052610
user1052610

Reputation: 4719

Maven Dependencies for Jersey @Context

I am trying to get the HttpServletRequest from a jersey ContainerRequestFilter, using @Context as follows:

import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

@Provider
public class MyFilter implements ContainerRequestFilter {

    @Context
    protected HttpServletRequest httpRequest;

    @Override
    public ContainerRequest filter(ContainerRequest containerRequest) {
       // logic ...
    }

}

The filter is invoked upon the incoming request, but httpRequest is always null.

Currently am using version 1.19.3 for both jersey-server and jersey-json. Am trying to move to a later version of jersey to see if this solves the issue. Have changed the jersey-server version to 2.7, however there is no corresponding jersey-json with the same version. Instead, am bringing in the dependency for jersey-media-json-jackson version 2.8, as follows:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.7</version>
    <exclusions>   <exclusion>
    <artifactId>asm</artifactId>
    <groupId>asm</groupId>
    </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.8</version>
</dependency>

However, this throws the following build error (same when using jersey-json version 1.19.3):

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class

What are the correct jersey dependencies which are required for @Context to successfully inject the HttpServletRequest object into the filter?

Thanks

Upvotes: 1

Views: 1510

Answers (1)

braunpet
braunpet

Reputation: 449

The annotation @Context is part of

<dependency>
   <groupId>javax.ws.rs</groupId>
   <artifactId>javax.ws.rs-api</artifactId>
   <version>2.0</version>
</dependency>

I am still using Jersey version 2.23.1 and @Context works fine. The lastest version is 2.26, which is probably the version you should start with.

You are probably looking for a library to serialise and deserialise JSON:

<dependency>
    <groupId>com.owlike</groupId>
    <artifactId>genson</artifactId>
    <version>1.4</version>
</dependency>

These are all my Jersey dependencies:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.23.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.23.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.23.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-declarative-linking</artifactId>
    <version>2.23.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.23.1</version>
</dependency>

Upvotes: 2

Related Questions