webservice:cxf整合spring入门

一,服务端

  1. 创建maven工程,打成war
  2. 在main/webapp下加入WEb-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
          <listener>
            <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>
          <servlet>
            <servlet-name>webservice</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
          </servlet>
          <servlet-mapping>
            <servlet-name>webservice</servlet-name>
            <url-pattern>/ws/*</url-pattern>
          </servlet-mapping>

          <welcome-file-list>
            <welcome-file>login.html</welcome-file>
          </welcome-file-list>
        </web-app>
3.在pom.xml中加入spring/cxf的依赖(注意jdk要用1.7的,1.5不支持webservice的一些功能)

            <properties>
                <spring.version>4.2.4.RELEASE</spring.version>
            </properties>

           <dependencies>
                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>${spring.version}</version>
                </dependency>

                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context-support</artifactId>
                  <version>${spring.version}</version>
                </dependency>

                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-web</artifactId>
                  <version>${spring.version}</version>
                </dependency>

                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-orm</artifactId>
                  <version>${spring.version}</version>
                </dependency>

                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-beans</artifactId>
                  <version>${spring.version}</version>
                </dependency>

                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-core</artifactId>
                  <version>${spring.version}</version>
                </dependency>

                <dependency> 
                    <groupId>org.apache.cxf</groupId> 
                    <artifactId>cxf-rt-frontend-jaxws</artifactId> 
                    <version>3.0.1</version> 
                </dependency> 
                <dependency> 
                    <groupId>org.apache.cxf</groupId> 
                    <artifactId>cxf-rt-transports-http</artifactId> 
                    <version>3.0.1</version> 
                </dependency>

                </dependencies>

          <build>
              <plugins>  
                  <plugin>  
                      <groupId>org.apache.maven.plugins</groupId>  
                      <artifactId>maven-resources-plugin</artifactId>  
                      <version>2.5</version>  
                  </plugin>  
                  <plugin>  
                      <groupId>org.apache.maven.plugins</groupId>  
                      <artifactId>maven-compiler-plugin</artifactId>  
                      <version>2.3.2</version>  
                      <configuration>  
                          <source>1.7</source>  
                          <target>1.7</target>  
                      </configuration>  
                  </plugin>  
              </plugins>  
           </build>

4.编写webService接口,在接口上加上注解@WebService
5.编写实现类
6.在src/main/resources里添加applicationContext_cxf.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.xsd                           
                                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
                <bean id="myWeather" class="com.lab.myweather.impl.MyWeatherImpl"></bean>

                <jaxws:server address="/weather">
                    <jaxws:serviceBean>
                        <ref bean="myWeather"></ref>
                    </jaxws:serviceBean>
                </jaxws:server>

                </beans>

7.综合web.xml,application_cxf.xml,访问路径为http://localhost/MyCXFService/ws/weather?wsdl
8.在浏览器上访问,wsdl:service name=”MyWeatherImplService”>知MyWeatherImpl为类名,wsdl:operation name=”getWeather”> 知getWeather为方法
wsdl:import location=”http://localhost/MyCXFService/ws/weather?wsdl=MyWeather.wsdl” namespace=”http://myweather.lab.com/”>
访问上面的路径http://localhost/MyCXFService/ws/weather?wsdl=MyWeather.wsdl

<xs:complexType name="getWeather">
        <xs:sequence>
        <xs:element minOccurs="0" name="arg0" type="xs:string"/>
        </xs:sequence>
        </xs:complexType>
        <xs:complexType name="getWeatherResponse">
        <xs:sequence>
        <xs:element minOccurs="0" name="return" type="xs:string"/>
        </xs:sequence>
        </xs:complexType>
        可以知道参数和返回值,带response的为返回值,不带的为参数,上例参数为String,返回值为String

客户端

  1. 创建maven工程,打成jar包
  2. 在src/main/java文件夹,shift+右键,在此处打开命令窗口
  3. 输入wsimport -s . http://localhost/MyCXFService/ws/weather?wsdl
  4. 回车
  5. 回到eclipse刷新src/main/java文件夹,可以看到生成了很多文件
  6. 添加一个与服务端一样的pom.xml
  7. 添加一个application_cxf.xml 注意下面serviceClass为生成的接口全路径
  8. 编写测试类测试就好
<?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.xsd                           
                                                                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

            <jaxws:client id = "weather" serviceClass="com.lab.myweather.impl.MyWeather" address="http://localhost/MyCXFService/ws/weather?wsdl">

            </jaxws:client>
            </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值