使用spring和Tomcat发布CXF WebService

Posted 学习笔记和总结

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用spring和Tomcat发布CXF WebService相关的知识,希望对你有一定的参考价值。

上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败。

cxf的WebService也可利用Tomcat来发布,并且使用8080端口。方法如下:

 

maven配置:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.0.RELEASE</spring.version>
        <cxf.version>3.0.3</cxf.version>
    </properties>

    <dependencies>
        <!--Spring框架核心库 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring Web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- cxf -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- 客户端调用restFul服务需要导入的包 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
            <version>${cxf.version}</version>
        </dependency>

---

在web.xml中加入以下配置(spring监听器和cxf的servlet):

    <listener>
        <description>SpringListener</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <!--cxf的Servlet-->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

---

接口:

@WebService
public interface HelloService{

    public String helloCxf();

    public String hello(String name);

    public User getUser(int id);

    public void saveUser(User user);
}

 

实现类:

@WebService(serviceName = "helloService",
        endpointInterface = "cn.lg.ws.hellocxf.HelloService"
)
public class HelloServiceImpl implements HelloService{

    @Override
    public String helloCxf(){
        return "Hello CXF!";
    }

    @Override
    public String hello(String name)
    {
        return "Hello " + name;
    }

    @Override
    public User getUser(int id) {
        User user = new User();
        user.setId(id);
        user.setName("lg");
        user.setEmail("QQ");
        user.setAddress("hahaha");
        return user;
    }

    @Override
    public void saveUser(User user) {
        System.out.println(user.toString());
    }

}

 

cxf-bean.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:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">

    <bean id="helloServiceBean" class="cn.lg.ws.hellocxf.HelloServiceImpl" />

    <jaxws:server id="helloServicej" serviceClass="cn.lg.ws.hellocxf.HelloService"
                  address="/hello">
        <jaxws:serviceBean>
            <ref bean="helloServiceBean" />
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

该文件需添加到spring配置文件中:   <import resource="cxf-bean.xml" />

 

浏览器访问 http://localhost:8080/webapp/cxf ,若出现以下界面则表示发布成功:

 

以上是关于使用spring和Tomcat发布CXF WebService的主要内容,如果未能解决你的问题,请参考以下文章

带有tomcat和cxf-servlet的spring-boot

CXF整合spring,在tomcat中发布webService

CXF+Spring+Tomcat 案例

不带弹簧的 CXF [关闭]

如何处理 URL(Spring、REST、CXF)中的转义字符(管道 |)?

系统开发系列 之WebService(spring框架+maven+CXF框架)部署问题及解决方案