CXF官方网站:http://cxf.apache.org/ 1.使用CXF发布WebService服务 准备: 导入相关jar包: Jar包下载地址:http://cxf.apache.org/download.html 或者使用maven导入: dependency groupIdorg.apache.cxf/groupId artifact
CXF官方网站:http://cxf.apache.org/
1.使用CXF发布WebService服务
准备:
导入相关jar包:
Jar包下载地址:http://cxf.apache.org/download.html
或者使用maven导入:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.6</version> </dependency>
创建一个用于发布WebService的类:
@WebService public class PhoneService { public Phone getPhoneInfo(String name) { Phone phone = new Phone(); phone.setName(name); if (name.endsWith("android")) { phone.setProduce("Oracle"); phone.setTime(new Date()); } else if (name.endsWith("apple")) { phone.setProduce("Apple"); phone.setTime(new Date()); } else { phone.setProduce("未知"); phone.setTime(new Date()); } return phone; } }
发布该服务:
public static void main(String[] args) { // 发布服务的类, 类似Endpoint JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean(); // 注册服务器地址和端口 jaxWsServerFactoryBean.setAddress("http://127.0.0.1:8888/wwss/phoneService"); // 注册哪个类提供服务 jaxWsServerFactoryBean.setServiceBean(new at.flying.ws.service.PhoneService()); //配置输入日志拦截器 jaxWsServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); //配置输出日志拦截器 jaxWsServerFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); // 发布一个cxf服务 jaxWsServerFactoryBean.create(); }
在浏览器地址栏输入上述代码设置的地址:http://127.0.0.1:8888/wwss/phoneService?wsdl
即可看到发布的服务的WSDL文件。
然后根据该文件生成本地Java代码,详细操作请参考该篇文档:http://my.oschina.net/u/2608182/blog/735762
然后根据本地Java代码写的测试函数如下:
@Test public void testws1() { PhoneServiceService phoneService = new PhoneServiceService(); PhoneService phoneService1 = phoneService.getPhoneServicePort(); //获取到生成的本地Phone类,就可以调用其中的任何对外开放的方法 Phone phone = phoneService1.getPhoneInfo("eeeeapple"); System.out.println(phone.getName()); System.out.println(phone.getProduce()); System.out.println(phone.getTime()); }
输出结果如下:
Tips:
从以上步骤我们可以看到使用CXF框架跟使用JDK来发布WebService服务有以下几点不同:
1.CXF需要导入额外的Jar包。
2.两者的发布方式不同。
2.CXF与Spring的集成
用于发布WebService的类:
@WebService @Service("phoneService") public class PhoneService { public Phone getPhoneInfo(String name) { Phone phone = new Phone(); phone.setName(name); if (name.endsWith("android")) { phone.setProduce("Oracle"); phone.setTime(new Date()); } else if (name.endsWith("apple")) { phone.setProduce("Apple"); phone.setTime(new Date()); } else { phone.setProduce("未知"); phone.setTime(new Date()); } return phone; } }
1.配置cxf的Servlet
<!--WebServcie-CXF配置Servlet start--> <servlet> <servlet-name>ws-cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ws-cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
2. cxf的spring配置文件
<?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 "> <!--最终的访问WSDL文件的地址是:http://localhost:8080/{1}/{2}/{3}?WSDL {1}:项目名,我这里是SSMProjectMaven {2}:在web.xml文件中配置org.apache.cxf.transport.servlet.CXFServlet时的匹配路径,我这里是ws {3}:以下<jaxws:server></jaxws:server>标签中的address属性值,我这里是phone 故我的WSDL访问地址是:http://127.0.0.1:8080/SSMProjectMaven/ws/phone?wsdl --> <jaxws:server id = "ws_phone" serviceClass = "at.flying.ws.service.PhoneService" address = "/phone"> <!--发布服务的Bean--> <jaxws:serviceBean> <ref bean = "phoneService" /> </jaxws:serviceBean> <!--配置输入日志拦截器--> <jaxws:inInterceptors> <bean class = "org.apache.cxf.interceptor.LoggingInInterceptor" /> </jaxws:inInterceptors> <!--配置输出日志拦截器--> <jaxws:outInterceptors> <bean class = "org.apache.cxf.interceptor.LoggingOutInterceptor" /> </jaxws:outInterceptors> </jaxws:server> </beans>
3.将cxf的Spring配置文件引入到Spring总配置文件中去:
<!--引入WebService-CXF配置文件--> <import resource = "at/flying/ws/spring/xml/ws-spring.xml" />
4.启动服务器后,Spring会自动发布服务,在浏览器中输入访问WSDL的地址,我这里是:http://127.0.0.1:8080/SSMProjectMaven/ws/phone?wsdl。即可看到相关服务的WSDL文档。
Tips:
访问“http://127.0.0.1:8080/SSMProjectMaven/ws/”地址时,会出现发布的WebService的详细信息,如下:
若需测试发布的WebService,则需要根据WSDL文件生成Java本地代码。具体细节参见该篇博客:http://my.oschina.net/u/2608182/blog/735762
CXF官方网站:http://cxf.apache.org/