webService使用总结

webService 是什么就不解释了,webservice有很多种开发方式,也有很多种调用方式,自己实际中用到的一种现在描述如下:

[color=darkblue]服务端的开发[/color]使用到apache CXF的开发方式,比较效率。

1.新建一个web项目,把apache-cxf-x.x.x\lib下的jar包添加到项目中,(加哪些看具体需要)

2.写好你的接口与实现类。(这里数据绑定方式使用cxf默认的jaxb,你也可以用其它的比如 aegis)
3.写好了之后就是发布服务了,发布方式也有好几种,这里采用的是cxf+spring整合在tomcat下发布的方式.具体的配置如下: applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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"
default-lazy-init="true">


<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:endpoint id="importDataEndPoint" implementor="#importDataService" address="/ImportDataService" />
<jaxws:endpoint id="dashboardDataEndPoint" implementor="#dashboardDataService" address="/DashboardDataService" />

<jaxws:endpoint id="inventoryEndPoint" implementor="#inventoryService" address="/InventoryService" />
<jaxws:endpoint id="emailEndPoint" implementor="#emailService" address="/EmailService" />

<jaxws:endpoint id="importDataDBPointLoadingEndPoint" implementor="#importDataDBPointLoadingService" address="/ImportDataDBPointLoadingService" />
<jaxws:endpoint id="dashboardDataCalculationEndPoint" implementor="#dashboardDataCalculationService" address="/DashboardDataCalculationService" />
<jaxws:endpoint id="upsModulesEndPoint" implementor="#upsModulesService" address="/UpsModulesService" />
<bean id="SpringContextHolder" lazy-init="false" class="com.accentrix.nttca.dcms.dashboard.util.SpringContextHolder" />

</beans>
<!-- END SNIPPET: beans -->



[color=violet]配置文件中只列出了跟cxf发布服务有关的,涉及到的接口名虽然原名复制了过来,但看看也没什么关系[/color]


当然,你还需要在你的web.xml文件中配置cxf的servlet,具体如下:


<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License. -->

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>




[color=blue]这样,发布服务就成了启动tomcat了,查看发布是否成功,看以访问这个链接:http://localhost:8080/xx/xx[/color]


[color=darkblue]客户端的调用[/color]

客户端调用有很多种形式,比如常见的可能是要用到AXIS来生成一堆客户端代码放进客户端程序,然后在客户端程序的配置文件中如下配置:xxxx.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="importDataService" serviceClass="xxxx.dashboard.service.ImportDataService"
address="http://xxxx:8080/xxxxx/ImportDataService" />

....



然后呢,在你的程序里就可以调用webservice的接口了,我写了一个调用接口的工具类:


import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CxfUtil {

public static <T> T getService(String name, Class<T> requiredType) {
return ApplicationContextHolder.context.getBean(name, requiredType);
}

public static <T> T getService(Class<T> requiredType) {
return ApplicationContextHolder.context.getBean(requiredType);
}

private static class ApplicationContextHolder {
private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "META-INF/xxxx.xml" });
}
}


这样,在你的代码中就可以如下实现调用接口方法了:

ImportDataService ds = CxfUtil .getService(ImportDataService.class);
ds.doxxx();
....


发布与调用的方式有很多种,看你的需要了。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值