2013-08-16 2 views
0

J'essaye de développer un service web en utilisant Apache CXF et en utilisant Spring pour gérer les beans. annd jetty comme mon serveur web.Comment injecter des beans de service gérés par ressort à Aplet CXF Servlet

Voici donc ma classe de ressources/WebService

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.MediaType; 

import org.springframework.stereotype.Component; 

    @Component 
    @Path("/test") 
    public class TestService{ 

     @GET 
     @Path("/add/{name}") 
     @Produces(MediaType.APPLICATION_JSON) 
     public String showName(@PathParam("name") String name){ 
      return name + ""; 
     } 


    } 

Mon Web.xml

<!-- Bean Declarations --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>WEB-INF/test-beans.xml</param-value> 
</context-param> 

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 


<servlet> 
    <servlet-name>CXFServlet</servlet-name> 
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>CXFServlet</servlet-name> 
    <url-pattern>/api/*</url-pattern> 
</servlet-mapping> 

Et test beans.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

    <context:component-scan base-package="com.test.ws" /> 

</beans> 

Comment puis-je intégrer alors mon printemps gérer le bean Service avec Apache CXF en tant que service web de repos?

Répondre

3

Ajouter un espace de noms à votre de fichier de configuration Spring:

xmlns:jaxrs="http://cxf.apache.org/jaxrs" 

et le schéma aussi lieu pour elle:

http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 

Aussi, vous allez avoir besoin de quelques dépendances pour CXF Rest service web:

<dependency> 
    <groupId>org.apache.cxf</groupId> 
    <artifactId>cxf-rt-frontend-jaxrs</artifactId> 
    <version>${cxf.version}</version> 
</dependency> 

Ensuite, configurez votre serveur JAX-RS dans votre configuration printan son:

<jaxrs:server id="yourJaxRsServer" address="/testService"> 
    <jaxrs:serviceBeans> 
     <ref bean="serviceBean"/> 
    </jaxrs:serviceBeans> 
</jaxrs:server> 

<bean id="serviceBean" class="service.TestService"/> 

Ne pas oublier d'enlever @Component annotation de votre classe TestService que vous déclarez dans .xml config. Ou si vous souhaitez conserver cette annotation pour une meilleure vue, ajoutez-y un nom @Component("testService"), puis vous pouvez supprimer la déclaration <bean id="serviceBean" class="service.TestService"/> de .xml et remplacer la référence par <ref bean="testService"/>.

Plus d'informations vous pouvez trouver à l'adresse:

+0

comment pourrais-je accéder à mon service alors? étant donné que le nom du projet est spring-cxf et que la servlet cxf est configurée comme/api/*? – user962206

1

Pour accéder à votre service Web, vous avez besoin de frapper l'URL suivante

http://<HOST>:<PORT>/<Application Context>/testService/api/test/add/<NAME_YOU_WANT_TO_ADD> 
Questions connexes