webservice之cxf服务端及客户端实现(maven创建web项目)

1. 运行环境

   (1) 操作系统:window10

   (2) JDK:1.7

   (3) IDE:Myeclipse10

   (4)服务器:tomcat7


2. cxf 服务端搭建步骤

   (1) 配置maven配置文件pom.xml, 加载cxf所需jar包

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  com.dh
  TestWebService
  war
  0.0.1-SNAPSHOT
  TestWebService
  http://maven.apache.org
  
 
  
  3.8.1
  4.3.9.RELEASE
  3.1.8


 

  
 
  


   org.springframework
   spring-webmvc
   ${spring.version}


 
 
 
org.apache.cxf 
cxf-rt-transports-http 
${cxf.version} 
 

 
 
org.apache.cxf 
cxf-rt-frontend-jaxws
${cxf.version} 
 
 
 
 
org.apache.cxf 
cxf-rt-transports-http-jetty 
${cxf.version} 

 
   
      junit
      junit
      ${junit-version}
   

    
 

  
 
    TestWebService
 


(2) 编写集成spring的配置文件springmvc.xml及cxf的配置文件spring-webservice.xml

A. springmvc.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util-4.3.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.3.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">





 
 
 















 



B. spring-webservice.xml

 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation="http://cxf.apache.org/jaxws 
http://cxf.apache.org/schemas/jaxws.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-lazy-init="true"> 

 
 

 
 
 

 
 



(3) 定义接口IMyWebService 及实现类MyWebServiceImpl

A. IMyWebService.java

package com.test.testservice.cxf.webservice.impl;

import javax.jws.WebService;

import com.test.testservice.cxf.webservice.IMyWebService;


@WebService(endpointInterface="com.test.testservice.cxf.webservice.IMyWebService")
public class MyWebServiceImpl implements IMyWebService {


@Override
public int add(int a, int b) {
System.out.println("----a+b=====" + (a+b));
return 0;
}


@Override
public int minus(int a, int b) {
System.out.println("----a-b=====" + (a-b));
return 0;
}

}


B. MyWebServiceImpl.java

package com.test.testservice.cxf.webservice.impl;

import javax.jws.WebService;

import com.test.testservice.cxf.webservice.IMyWebService;


@WebService(endpointInterface="com.test.testservice.cxf.webservice.IMyWebService")
public class MyWebServiceImpl implements IMyWebService {


@Override
public int add(int a, int b) {
System.out.println("----a+b=====" + (a+b));
return 0;
}


@Override
public int minus(int a, int b) {
System.out.println("----a-b=====" + (a-b));
return 0;
}
}


(4) 发布应用到tomcat,在浏览器中访问:http://localhost:8080/TestWebService/server/web-publish?wsdl ,即可查看接口信息


3.cxf 客户端调用(与cxf 服务端在同一应用中)

package com.test.testservice.cxf.webservice;


import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


public class ClientForCXF {

public static IMyWebService getInterFace() {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(IMyWebService.class);
factoryBean.setAddress("http://localhost:8080/TestWebService/server/web-publish");
return (IMyWebService) factoryBean.create();
}


public static void main(String[] args) {
IMyWebService myWebService = getInterFace();
System.out.println("client: " + myWebService.add(1, 3));
}

}















你可能感兴趣的:(webservice之cxf服务端及客户端实现(maven创建web项目))