2013-08-16 58 views
0

我正在嘗試使用Apache CXF開發Web服務並使用Spring來管理Bean。 annd碼頭作爲我的網絡服務器。如何將Spring管理的服務bean注入Apache CXF Servlet

因此,這裏是我的資源/ 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 + ""; 
     } 


    } 

我的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> 

和測試的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> 

我怎麼會那麼整合我的春節使用Apache CXF作爲Rest Web服務管理Service bean?

回答

3

命名空間添加到您的Spring配置文件:

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

,並架構位置吧:

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

你也將需要爲CXF REST Web服務的一些相關性:

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

然後在你的Spring配置中配置你的JAX-RS服務器,就像t他:

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

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

不要忘了,你在.xml配置宣稱它從你的TestService類中刪除@Component註解。或者,如果您想保留此註釋以獲得更佳視圖,請爲其添加一個名稱@Component("testService"),然後您可以從.xml中刪除<bean id="serviceBean" class="service.TestService"/>聲明並將參考更改爲<ref bean="testService"/>

的更多信息,你可以在這裏找到:

+0

我怎麼會訪問我的服務呢?因爲項目名稱是spring-cxf,並且cxf servlet被配置爲/ api/*? – user962206

1

要訪問您的Web服務,您需要點擊以下網址

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