Apache-CXF开发Webservice学习笔记

公司前辈们开发webservice服务架构之前都是在用C#去做。

作为新手开始学习使用Java开发webservice,结合网上的学习资料选择了Apache的CXF。

现已开发完成并应用,记录一下开发过程中的学习笔记。

了解Apache-CXF

Apache CXF是一个开源的Service框架,可以用于简化用户的service开发,

基于CXF开发的应用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服务。

CXF底层页可以使用不同的传输协议,包括HTTP、JMS或JBI等。

支持大量的Web Service标准,包括SOAP、WS-I Basic Profile、WSDL、

WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。

Webservice开发

搭建项目架构,使用maven项目管理+Spring整合Mybatis

添加CXF的maven依赖:

  <properties>
    <!-- cxf版本号 -->
    <cxf.version>2.7.7</cxf.version>
  </properties>

<dependencies>
    <!-- cxf的开发包 -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf</artifactId>
        <version>${cxf.version}</version>
        <type>pom</type>
    </dependency>
    <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-transports-http-jetty</artifactId>
        <version>${cxf.version}</version>
    </dependency>
</dependencies>

CXF中采用注解的方式声明哪些类作为WebService进行发布,

@WebService:声明webservice接口;

@WebService(endpointInterface="com.test.TestService"):声明这个类是TestService接口的实现类。

配置spring-mybatis.xml文件时整合cxf的配置:

    <import resource="classpath:/META-INF/cxf/cxf.xml"/>
    <import resource="classpath:/META-INF/cxf/cxf-extension-soap.xml"/>
    <import resource="classpath:/META-INF/cxf/cxf-servlet.xml"/>
    
 
    <cxf:server serviceClass="com.test.service.TestService" address="/hello">
        <cxf:serviceBean>
            <bean class="com.test.service.impl.TestServiceImpl"/>
        </cxf:serviceBean>
    </cxf:server>

发布webservice应用程序
        serviceClass属性 : 指向当前需要发布的webservice应用实现的接口
        address属性 : 描述当前发布的webservice程序唯一地址,当其他工程需要访问本工程发布的某一个webservcie程序时,需要通过这个唯一地址进行访问
        serviceBean : 指向当前发布的webservice程序的具体实现类

配置web.xml文件:

  <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

这里webservice可以通过tomcat验证是否发布成功。

在开发过程中遇到的一些问题:


学习maven项目打包发布war包部署到服务器上

        <plugin>  
	        <groupId>org.apache.maven.plugins</groupId>  
	        <artifactId>maven-resources-plugin</artifactId>  
	        <configuration>  
	            <encoding>UTF-8</encoding>  
	        </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
这里将jar包依赖和resource配置文件一起打包发布。


发布了13 篇原创文章 · 获赞 1 · 访问量 8242

猜你喜欢

转载自blog.csdn.net/romanticRose/article/details/75072916