如何将Spring托管服务bean注入Apache CXF Servlet

我正在尝试使用Apache CXF开发Web服务并使用Spring来管理bean . 和码头作为我的网络服务器 .

所以这是我的Resource / 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>

和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>

然后,我如何将我的Spring管理服务bean与Apache CXF集成为Rest Web服务?

回答(2)

3 years ago

在您的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服务器,如下所示:

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

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

当您在 .xml config中声明它时,不要忘记从 TestService 类中删除 @Component 注释 . 或者,如果您想保留此注释以获得更好的视图,请为其添加名称 @Component("testService") 然后您可以从 .xml 中删除 <bean id="serviceBean" class="service.TestService"/> 声明并将引用更改为 <ref bean="testService"/> .

您可以在以下位置找到更多信息

3 years ago

要访问您的Web服务,您需要点击以下URL

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