服务器端:
1.新建Web项目,例如CXF_Server,导入cxf-2.4.2的相关jar包,如下图所示:
2.新建一个webservice服务接口MyService,该接口通过注解来暴露服务:package com.founder.service;
import javax.jws.WebService;
@WebService(serviceName="MyServiceManage")
public interface MyService {
/**
* add():定义add方法
* @param a
* @param b
* @return
*/
public int add(int a,int b);
}
3.写出该接口的实现类MyServiceImpl:package com.founder.service;
public class MyServiceImpl implements MyService {
@Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a + b));
return a+b;
}
}
4.配置spring,把webservice类托管给spring,然后在src下新建spring的配置文件applicationContext.xml:<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
5.配置web.xml:<?xml version="1.0" encoding="UTF-8"?>
CXF__Server
cxfServlet
org.apache.cxf.transport.servlet.CXFServlet
0
cxfServlet
/ws/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
至此服务器端基本上告一段落,可以将应用部署到tomcat,启动并访问http://localhost:8080/CXF_Server/ws/myService?wsdl,如果能正确显示xml文件则说明部署成功。
客户端测试:
1.通过myeclipse自带的soap浏览器完成测试,就不用在客户端写java代码了:
点击图标所示按钮,即可启动soap浏览器。
2.点击右侧图标所示的按钮wsdl page,在输入框中输入wsdl地址:
3.点击里面的add()方法,输入参数,点击go,即可显示调用结果:
OK,实例完成!