How do I migrate from Jersey 1.0 to Jersey 2.0?

前端 未结 5 1397
旧时难觅i
旧时难觅i 2021-02-05 06:07

I\'m trying to upgrade to Jersey 2.0 and I\'m having a lot of trouble because the groupIds and artifactIds of Jersey have completely changed and I can\'t find a migration plan i

5条回答
  •  滥情空心
    2021-02-05 06:48

    You can follow following steps for migration from Jersey 1 to Jersey 2 :

    Add following dependencies in POM file : Jersey 2.23.2 dependencies

       
        org.glassfish.jersey.containers
        jersey-container-servlet-core
        2.23.2
       
       
        org.glassfish.jersey.ext
        jersey-spring3
        2.23.2
        
         
          org.springframework
          spring-beans
         
         
          org.springframework
          spring-core
         
         
          org.springframework
          spring-web
         
        
       
       
        org.glassfish.jersey.core
        jersey-client
        2.23.2
       
       
        org.glassfish.jersey.media
        jersey-media-moxy
        2.23.2
       
       
        org.glassfish.jersey.ext
        jersey-entity-filtering
        2.23.2
       
       
        org.glassfish.jersey.core
        jersey-server
        2.23.2
       
       
        org.glassfish.jersey.core
        jersey-common
        2.23.2
       
       
        org.glassfish.jersey.bundles.repackaged
        jersey-guava
        2.23.2
       
       
          org.glassfish.jersey.media
          jersey-media-json-jackson
          2.23.2
          
           
            com.fasterxml.jackson.core
             jackson-annotations
           
          
       
       
          com.fasterxml.jackson.core
          jackson-annotations
          2.5.4
       
       
           org.glassfish.jersey.media
           jersey-media-multipart
           2.23.2
       
    
       
           org.jvnet
           mimepull
           1.6
       
    

    Make Following entry in Web.xml :

    
    
    
     
      jersey-servlet
      org.glassfish.jersey.servlet.ServletContainer
      
       javax.ws.rs.Application
       com.jsg.resource.initializer.RestResourceInitializer
      
      1
     
     
      jersey-servlet
      /rest/*
      '
    
           
      org.springframework.web.context.ContextLoaderListener
     
     
      contextConfigLocation
      classpath:applicationContext.xml
     
          
            DB Connection
            jdbc/myAppName
           javax.sql.DataSource
           Container
       
    
    

    Write following code in RestResourceIntializer

        package com.jsg.resource.initializer;
    
        import java.util.HashSet;
        import java.util.Set;
        import javax.ws.rs.core.Application;
    
        public class RestResourceInitializer extends Application {
    
         /**
          * Gets the classes.
          *
          * @return the classes
          */
         public Set> getClasses() {
          Set> classes = new HashSet>(); 
    
          // Resources
          classes.add(org.glassfish.jersey.jackson.JacksonFeature.class);
          classes.add(org.glassfish.jersey.server.spring.scope.RequestContextFilter.class);
          classes.add(org.glassfish.jersey.media.multipart.MultiPartFeature.class);
    
                       //Rest classes within Application.
                        classes.add(com.jsg.rest.AbcRestService.class);
    
          classes.add(com.jsg.rest.CdeRestService.class);
          return classes;
         }
        }
    

    Now if you will deploy code with above changes on websphere,you will get following exception :

    Caused by: java.lang.NoSuchMethodError: javax/ws/rs/core/Application.getProperties()Ljava/util/Map; at org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:287) at org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:311)
    

    Reason for above exception is that,Websphere supports JAX-RS 1 implementation,however we are deploying Jersey 2 code which is Jax-rs 2 implementation.

    Steps for resolving above exception:

    So basically what we have to do is to force WebSphere to pick our Jersey 2 jars instead of default Jax-rs 1.We need to follow following steps for that

    1) Disable in built JAX-RS by setting following JVM property to true

    com.ibm.websphere.jaxrs.server.DisableIBMJAXRSEngine=true
    

    This property can be set through admin console of WebSphere by going to Servers->All Server -> ->Server Infrastructure -> Java and Process Management ->Process Deifinition ->Additional Properties-> Java Virtual Machine ->Additional Properties-> Custom Properties

    2) Create Isolated Shared Library having the Jersey 2 Jars and Spring 4 Jars Isolated shared library can be created through admin Console of Websphere by going to Environment-> Shared Libraries ->New

    n the classpath box,we need to enter path of the folder on the server,where we have placed all Jersey 2 and Spring 4 Jars

    /var/was/server2/jerseyLib1/spring-context-4.3.4.RELEASE.jar
    /var/was/server2/jerseyLib1/spring-core-4.3.4.RELEASE.jar
    /var/was/server2/jerseyLib1/spring-beans-4.3.4.RELEASE.jar
    /var/was/server2/jerseyLib1/spring-aop-4.3.4.RELEASE.jar
    /var/was/server2/jerseyLib1/spring-web-4.3.4.RELEASE.jar
    /var/was/server2/jerseyLib1/spring-expression-4.3.4.RELEASE.jar
    /var/was/server2/jerseyLib1/spring-bridge-2.5.0-b05.jar
    /var/was/server2/jerseyLib1/hk2-locator-2.5.0-b05.jar
    /var/was/server2/jerseyLib1/hk2-api-2.5.0-b05.jar
    /var/was/server2/jerseyLib1/hk2-utils-2.5.0-b05.jar
    /var/was/server2/jerseyLib/javax.inject-2.5.0-b05.jar
    /var/was/server2/jerseyLib1/javax.annotation-api-1.2-b03.jar
    /var/was/server2/jerseyLib1/javax.ws.rs-api-2.0.1.jar
    /var/was/server2/jerseyLib1/jersey-client-2.23.2.jar
    /var/was/server2/jerseyLib1/jersey-spring3-2.23.2.jar
    /var/was/server2/jerseyLib1/jersey-container-servlet-core-2.23.2.jar
    /var/was/server2/jerseyLib1/jersey-server-2.23.2.jar
    /var/was/server2/jerseyLib1/jersey-common-2.23.2.jar
    
    /var/was/server2/jerseyLib1/jersey-guava-2.23.2.jar
    

    Also in class loading section ,select "use an isolated class loader for this shared library"

    and then finally click on Apply and Ok and we are done with creation of isolated shared library.

    1. Bind this isolated shared library with your application war file as follows in admin Console

      a) Application -> All Applications -> Click on your application name b) Go to References -> Shared Library References -> Reference Shared Libraries ->select your application war(Not ear) and click ok. c) Select the library that we created in Step 2 in "Available" combo box on left side and put it on right side in "Selected" combo box and click ok. With this we have associated the isolated shared library with application war file.

    2. Restart Server and application should be up and running.

提交回复
热议问题