webService使用总结
webService 是什么就不解释了,webservice有很多种开发方式,也有很多种调用方式,自己实际中用到的一种现在描述如下:
服务端的开发使用到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" /></beans><!-- END SNIPPET: beans -->
<?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>
<?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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="importDataService" service/> ....
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(); ....