Konfiguracja Spring 4 z Jersey

1

Potrzebuję podnieść wersję springa w moim projekcie(obecnie mam 3.2 chcę 4). Używam jerseya. Nie mogę sobie poradzić z przekonfigurowaniem poma i webxml. Zgodnie z tym co piszą w necie trzeba zmienić wersję jerseya na kompatybilną. przykład
W moim pomie mam:

<dependency>
			<groupId>com.sun.jersey.contribs</groupId>
			<artifactId>jersey-spring</artifactId>
			<version>1.18.1</version>
			<!-- jersey-spring uses spring 2.5, we want to use 3.x instead -->
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-core</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-web</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-beans</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-context</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-aop</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

W web.xml mapowanie rest servletu:

<servlet>
        <servlet-name>RestService</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.rest</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>RestService</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

Zmieniam dependecy jerseya na

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.12</version>

Ale nie wiem na jaką klasę servletu zmienić w web.xml i ustawić pakiet restowy bo nie mogę znaleźć konkretnej dokumentacji w tym temacie... przy próbie zmian dostaję z aplikacji error 404 przy próbie odwołania się do zasobu.

Jest ktoś obeznany w temacie?

0

Mam doświadczenie w użyciu Jersey (JAX-RS 2.0) ze Spring 4. Działa nieźle. Mój pom.xml wygląda następująco:

    <dependencies>
        
        <!-- drivers -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
        </dependency>
        <!-- end drivers -->
        
        <!-- utils -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- end utils -->
        
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>        
                
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>             
        <!-- end spring -->
        
        <!-- spring data -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>
        <!-- end spring data -->
        
        <!-- spring security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <!-- end spring security -->
        
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <!-- end jdbc -->
        
        <!-- JAX-RS -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.16</version>
        </dependency>
        
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-jaxb</artifactId>
            <version>2.16</version>
        </dependency>
        
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-processing</artifactId>
            <version>2.16</version>
        </dependency>
        
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.16</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.16</version>
        </dependency>
        <!-- end JAX-RS -->
        
        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.8.Final</version>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.8.Final</version>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.3.8.Final</version>
        </dependency>
        <!-- end hibernate -->        
    </dependencies>

Mój config aplikacji wygląda następująco:


@ApplicationPath("/rest")
public class RestAppConfig extends Application {
   @Context
   protected ServletContext servletContext;

   @Override
   public Set<Object> getSingletons() {
      WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);  
      AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();
      Set set = new HashSet();
      // dodaje singleton
      set.add(bf.getBean("samplers"));
      return set;
  }
}

Zwróc uwagę, że rejestruje bean jersey tak, aby był widoczny w springu. W tym celu wyszukuje singletona na podstawie nazwy w Springu, dzieki temu JAX-RS zostaje powiazany z wlasciwym obiektem, zarzadzalnym przez Springa.

Kontroler JAX-RS jest oznaczony jako obiekt zarządzalny przez Springa za pomocą @Conponent.

@Path("/samplers")
@Produces("application/json")
@Consumes("application/json")
@Component("samplers")
public class RestWebService implements IRestWebService {

    @Autowired
    private ISampleRepository sampleDao;
    @Context
    private ServletContext servletContext;

    @Path("/findall")
    @GET
    @Override
    public List<SampleObject> findAll() {
        return sampleDao.findAll();
    }

    // czesc kodu pominieto

}

Aplikacja byla deployowana na Tomcata.

1 użytkowników online, w tym zalogowanych: 0, gości: 1