0

I am using glassfish Jersey JAX-RS on Java 11 and Tomcat 9 server. I upgraded my Jersey version to 2.27 recently after @Context object is coming null in my application. This object belongs to a 3rd party API.

The version of Jersey in the 3rd party API, having the context element, is 1.19 and also, it belongs to com.sun package and not the one I am using which is org.glassfish. My called Rest method looks like:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Pageable<Role> getRoles(@Context final Search search) throws ServerException {

    return new Pageable<>(identityManagementService.getRoles(), search);
}

which calls following :

public Pageable(final List<T> list, final Search search) {
    this.count = list.size();
    this.data = list;
    if (search.getLimiter() != null && search.getLimiter().getCount() != 0) {
        this.data = list.stream()
               .skip(search.getLimiter().getOffset())
                .limit(search.getLimiter().getCount())
                .collect(Collectors.toList());

} }

3rd party Pom.xml has following entry :

<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19</version>
        <optional>true</optional>
        <exclusions>
            <exclusion>
                <artifactId>asm</artifactId>
                <groupId>asm</groupId>
            </exclusion>
        </exclusions>
    </dependency>

I have following entry in my pom.xml:

<dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.27</version>
        </dependency> 

The Search object is coming null at the run-time, causing my application to fail. Is there any compatibility issue between the two jersey versions or am I missing something. Is anyone already using Jersey JAX-RS 2.27 and Java 11 together ?

Aditya Batra
  • 263
  • 1
  • 2
  • 18
  • @cassio-mazzochi-molin could you help – Aditya Batra Jan 15 '19 at 10:29
  • Why @Context is need here?.Could you please share your request.May be u need to add @Consumes(MediaType.APPLICATION_JSON). – Prasobh.Kollattu Jan 15 '19 at 15:44
  • @Prasobh.K It is a custom context object, it is like bean injection in spring. I am using this Search context for filtering of the request as you can see in the above code's Pageable method. As far as request is concerned it is a simple get request with queryparams which are mapped to Search object. The get uri is : https://localhost:8080/App/rest/api/admin/roles?max=20&off=0 – Aditya Batra Jan 16 '19 at 07:18
  • Context is used to inject container managed objects.Please check https://stackoverflow.com/questions/38719533/understanding-rest-apis-what-are-context-and-context. If you have to get value in Search object, u have to sent corresponding JSON in the request. – Prasobh.Kollattu Jan 16 '19 at 11:16
  • @Prasobh.K yes thats true, but you can have your custom context providers as well. You may refer to this https://community.liferay.com/blogs/-/blogs/rest-custom-context-providers. I am sending two parameters as query params in request which are properties of Search object. – Aditya Batra Jan 16 '19 at 11:51

1 Answers1

0

The issue got resolved. I was referring to AbstractBinder of HK2 package after updating my Jersey to 2.27. The new version on Jersey Jax-RS got decoupled from HK2. Importing org.glassfish.jersey.internal.inject.AbstractBinder and implementing corresponding overridden methods solved the issue. The answer posted here Jersey HK2 Dependency Injection doesn't work after update to v2.27 helped me in reolving the issue.

Thanks

Aditya Batra
  • 263
  • 1
  • 2
  • 18