CXF开发webservice与Spring的整合1…

1、引入相关jar,所需jar如下

    下载地址:http://download.csdn.net/detail/luguling200802544/8415987

CXF开发webservice与Spring的整合1______简单类型返回与传值
2、开发服务端

  • 第一步,新建webservice接口

  package com.vsc.webservice;


import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public interface TestService {

 //无返回值
 public void  testHello();
 
 //返回string
 public String  testHello1();
 
 //返回int
 public int  testHello2();
 
 //接收参数  ,返回
 public void  testHello4(@WebParam(name="name") String name);
 
}

  • 接口实现类

package com.vsc.webservice.impl;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.vsc.webservice.TestService;


@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class TestServiceImpl implements TestService {

 public void testHello() {

  System.out.print("testHello 无返回");
 }

 public String testHello1() {
  System.out.print("testHello1");
  return "testHello1";
 }

 public int testHello2() {
  System.out.print("testHello2");
  return 3;
 }

 

 public void testHello4(String name) {
  
  System.out.print("testHello4  name="+name);

 }

}

  • 服务端配置,cxfServer.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:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://cxf.apache.org/jaxws
           http://cxf.apache.org/schemas/jaxws.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     《!-- 引入cxf配置的3个xml文件 -->
     《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"/>
  
      《!-- 配置bean -->
      《bean id="testBean" class="com.vsc.webservice.impl.TestServiceImpl">《/bean>
      《!-- 输入输出日志 -->
      《bean id="interceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor">《/bean>
      《bean id="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor">《/bean>
     
      《!-- 服务端配置 -->
      《jaxws:server id="testService" serviceClass="com.vsc.webservice.TestService" address="/test">
         《jaxws:serviceBean>
            《ref bean="testBean"/>
         《/jaxws:serviceBean>
         《jaxws:inInterceptors>
             《ref bean="interceptor"/>
         《/jaxws:inInterceptors>
         《jaxws:outInterceptors>
             《ref bean="outInterceptor"/>
         《/jaxws:outInterceptors>
     《/jaxws:server>

     《!--等价于上面的《jaxws:server》配置 --》
     《!--
     《jaxws:endpoint id="testService" implementor="#testBean" address="/test" >
        《jaxws:inInterceptors> 《ref bean="interceptor"/>《/jaxws:inInterceptors>
        《jaxws:outInterceptors>《ref bean="outInterceptor"/>《/jaxws:outInterceptors>
     《/jaxws:endpoint>
     -->
《/beans》

 

  • web.xml配置


    《web-app version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
     
     《context-param>
        《param-name>contextConfigLocation《/param-name>
        《param-value>classpath:cxfServer.xml《/param-value>
     《/context-param>
     《!-- 加载spring容器的配置文件 -->
     《listener>
         《listener-class>org.springframework.web.context.ContextLoaderListener《/listener-class>
     《/listener>
     
     《!-- 处理由JavaBeans Introspector的使用而引起的缓冲泄露 -->
     《listener>
        《listener-class>org.springframework.web.util.IntrospectorCleanupListener《/listener-class>
     《/listener>
     
     《!-- cxf 配置 -->
     《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> /* 《url-pattern>
     // int aa= testService.testHello2();
     // System.out.println("aa="+aa);
     // testService.testHello4("123");
      
     }

    }

  • 3、客户端开发,客户端与服务端分离,新建web项目,SceneMan,引入上述jar

    • 第一步,新建与服务端相同的接口,接口包路劲与服务端相同

    package com.vsc.webservice;


    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;

    @WebService
    @SOAPBinding(style=SOAPBinding.Style.RPC)
    public interface TestService {

     //无返回值
     public void  testHello();
     
     //返回string
     public String  testHello1();
     
     //返回int
     public int  testHello2();
     
     //接收参数  ,返回
     public void  testHello4(@WebParam(name="name") String name);
     
    }

    • 第一种调用,测试

    public class TestWebService {
     
     public static void  main(String args[]){
      
      JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
      factory.setServiceClass(TestService.class);
      factory.setAddress("http://localhost:8080/testWebService/test");
      TestService testService= (TestService) factory.create();
      testService.testHello1();
      
      
     // int aa= testService.testHello2();
     // System.out.println("aa="+aa);
     // testService.testHello4("123");
      
     }

    }

    • 第二种调用测试,使用配置,cxfClient.xml


    《beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jaxws="http://cxf.apache.org/jaxws"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://cxf.apache.org/jaxws
               http://cxf.apache.org/schemas/jaxws.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">

         《!-- 引入cxf配置的3个xml文件 -->
         《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"/>
      
       《jaxws:client id="serviceClient" serviceClass="com.vsc.webservice.TestService"
               address="http://localhost:8080/testWebService/test">《/jaxws:client>
    《/beans>

    • 测试

    public class TestWebService {
     
     public static void  main(String args[]){
      
      
      ApplicationContext application=new ClassPathXmlApplicationContext("cxfClient.xml");
      TestService testService=(TestService) application.getBean("serviceClient");
      int aa= testService.testHello2();
        System.out.println("aa="+aa);
     }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值