很早很早之前,就初步学习了WebService,感觉还是比较“好”的。
  使用Web服务,感觉就像普通API一样,和HTTP接口比较起来。
  WebService有个很大的局限,就是事务,分布式事务麻烦程度就上升了不少,暂时没有搞过。
  
  最近1年做的2个比较完整的项目,都有WebService的配置,只不过,都是别人配置好的。
  
  别人的,终究是别人的。
  
  作为一名热爱学习并学以致用的程序员,我也来亲自搞个配置~
  
  下面的例子,是我今天亲自一步步配的,而且运行成功了。
  
  CSDN下载地址:http://download.csdn.net/detail/fansunion/9218657
  (我打算今后在这个基础上,进一步完善示例)
  
 一、WebServiceClient接口项目
    其它项目如果想使用Web服务,直接调用接口就行了,根本不需要关注服务的实现。

	@WebService
public interface UserFacade {
String query();
}

接口,已经做到最简了。查询一个字符串,就表明Web服务调用成功了。
   
 

 package cn.fansunion.webservice.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.fansunion.webservice.UserFacade; public class WebServiceTest { public static void main(String[] args) {
//初始化Spring上下文,webservice文件位于src目录下(也可以说是Classpath下)
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:/spring-webservice.xml");
//根据id获得bean,我感觉是这个“jaxws:client id="remoteUserFacade"”WebService语法和Spring语法的结合
UserFacade userFacade=(UserFacade) ac.getBean("remoteUserFacade");
//查询,返回并打印字符串“WebService”
System.out.println(userFacade.query());
} }

spring-webservice.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 启用autowire -->
<context:annotation-config />
<context:component-scan base-package="com.fansunion.webservice" />
<jaxws:client id="remoteUserFacade"
address="http://localhost:8080/webService/UserFacade" serviceClass="cn.fansunion.webservice.UserFacade" /> </beans>

比较关键的是jaxws:client id="remoteUserFacade",这个是WebService的核心配置。
  另外,需要注意http://cxf.apache.org/jaxws的xsi和xmlns的配置,如果没有,应该会报错的。
  
  pom.xml配置
 

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>

二、WebServiceImpl接口的实现项目

package cn.fansunion.webservice.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Service;

import cn.fansunion.webservice.UserFacade;

@WebService(endpointInterface = "cn.fansunion.webservice.UserFacade", serviceName = "UserFacade")
@Service("userFacadeImpl")
public class UserFacdeImpl implements UserFacade { @Override
public String query() {
return "WebService";
} }

和普通的Java接口实现类相比,就多了WebService注解。
看样子,endpointInterface和serviceName很关键。
目前还不太清楚这2个属性是否是必须的,根据我已有的经验猜测,都是可选的,如果不写,会按照一定的规则用默认的名字和类。
刚刚写完,我就觉得不对了。
endpointInterface按说是必选的,当然如果不写,程序是完全可以分析得出来的,因为有“implements”。
有兴趣的,自己百度。

UserFacdeImpl需要作为Web的方式发布,接下来配置web.xml文件。
Spring配置文件,基本都懂,主要就是配置了WebService的CXFServlet。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-webservice.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webService/*</url-pattern>
</servlet-mapping> </web-app>

spring-webservice.xml

<!-- Import CXF -->
<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" /> <bean id="userFacade"
class="cn.fansunion.webservice.impl.UserFacdeImpl">
</bean>
<jaxws:server id="webserviceUserFacade" serviceBean="#userFacade" address="/UserFacade">
</jaxws:server>
<!-- 启用autowire -->
<context:annotation-config />
<context:component-scan base-package="com.fansunion.webservice" />

引入CXF的xml配置,配置bean,最重要的还是“jaxws:server”,和Client项目的“jaxws:client”相对应吧~

pom.xml
和之前的完全一致~

三、简要回顾下
UserFacadeClient项目:就一个接口UserFacade
UserFacadeImpl项目:接口实现UserFacadeImpl、spring-webservice.xml、pom.xml、web.xml
测试项目:WebServiceTest、pom.xml、spring-webservice.xml
为了方便,我们把测试项目,直接和Client接口项目放在了一起~

四、测试和运行流程
1.启动UserFacadeImpl这个Web项目
  2015-10-27 22:39:02.195:INFO:oejs.Server:jetty-8.1.14.v20131031
2015-10-27 22:39:02.562:INFO:/:No Spring WebApplicationInitializer types detected on classpath
2015-10-27 22:39:03.087:INFO:/:Initializing Spring root WebApplicationContext
十月 27, 2015 10:39:03 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
十月 27, 2015 10:39:03 下午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Oct 27 22:39:03 CST 2015]; root of context hierarchy
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-webservice.xml]
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-extension-soap.xml]
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-servlet.xml]
十月 27, 2015 10:39:04 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://impl.webservice.fansunion.cn/}UserFacade from class cn.fansunion.webservice.UserFacade
十月 27, 2015 10:39:04 下午 org.apache.cxf.bus.spring.OldSpringSupport logWarning
WARNING: Import of META-INF/cxf/cxf-extension-soap.xml has been deprecated and is unnecessary.
十月 27, 2015 10:39:04 下午 org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be /UserFacade
十月 27, 2015 10:39:04 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1644 ms
2015-10-27 22:39:04.839:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
------------------------------------------------
Jetty startup finished in 3.1 s.
Used memory: 5.5 MB of 121.8 MB (1.8 GB maximum)
Console available: type "help".
------------------------------------------------

2.运行WebServiceTest这个Java应用程序
十月 27, 2015 10:39:08 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2484e723: startup date [Tue Oct 27 22:39:08 CST 2015]; root of context hierarchy
十月 27, 2015 10:39:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-webservice.xml]
十月 27, 2015 10:39:09 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://webservice.fansunion.cn/}UserFacadeService from class cn.fansunion.webservice.UserFacade
WebService

3.也可以访问http://localhost:8080/webService/UserFacade/query?wsdl
查看这个WebService的定义

我想通过浏览器访问http://localhost:8080/webService/UserFacade/query?wsdl类似的URL,直接显示返回结果。
好像不太行哦,后面再研究吧~

五、最后说明
1.为了简单起见,这2个项目都非常地简单,完全没有与WebService无关紧要的代码和配置。
麻雀虽小,五脏俱全哦
2.为了简单起见,服务器用的是localhost,而且是写死的,你懂的~
3.看服务和应用程序的启动日志,是非常有价值的~

这次就先总结到这吧~

六、源码下载地址
http://download.csdn.net/detail/fansunion/9218657

小雷FansUnion-程序员一枚
2015年10月27日
湖北-武汉-循礼门
QQ:240370818
微信:FansUnion

Java-Spring-WebService最基础的配置示例的更多相关文章

  1. 在上已个Java Spring MVC项目基础上加MyBatis

    代码目录: /Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC 1. 首先在resource目录加上jdbc.properties: d ...

  2. Spring dbcp连接池简单配置 示例

    一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...

  3. Spring操作指南-IoC基础环境配置(基于注解自动装配)

    项目源码:http://code.taobao.org/p/LearningJavaEE/src/LearningSpring001%20-%20Automatically%20wiring%20be ...

  4. Spring操作指南-IoC基础环境配置(基于注解手动装配)

    Source: http://code.taobao.org/p/LearningJavaEE/src/LearningSpring002%20-%20Wiring%20beans%20with%20 ...

  5. Spring操作指南-IoC基础环境配置(基于XML)

  6. 将 Java Spring Framework 应用程序迁移到 Windows Azure

    我们刚刚发布了一个新教程和示例代码,以阐述如何在Windows Azure中使用 Java 相关技术.在该指南中,我们提供了分步教程,说明如何将 Java Spring Framework 应用程序( ...

  7. 学习笔记_J2EE_SSM_01_spring+springMVC+Mybatis整合_XML配置示例

    spring+springMVC+Mybatis整合_XML配置示例 1.概述 spring+springMVC+Mybatis整合  XML配置方式 1.1 测试环境说明 名称 版本 备注 操作系统 ...

  8. 【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑

    编写Java Spring Boot应用,通过配置logging.path路径把日志输出在指定的文件夹中. 第一步:通过VS Code创建一个空的Spring Boot项目 第二步:在applicat ...

  9. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

随机推荐

  1. 二、Fast-R-CNN

    一.概括 Fast R-cnn的主要亮点有:Fast R-CNN将借助多任务损失函数,将物体识别和位置修正合成到一个网络中,不再对网络进行分步训练,不需要大量内存来存储训练过程中特征的数据:用RoI层 ...

  2. Https个人总结

    花了一个星期终于搞懂了.. HTTPS个人总结: 一.RSA算法 公钥:可以分发给任意的钥匙 私钥:自己保留起来,不分发给别人的钥匙 RSA算法: 找出质数p.q n = p*q Φ(n)=(p-1) ...

  3. 在angular4.X里使用mCustomScrollbar滚动条插件

    参考网上的方法https://stackoverflow.com/questions/36755625/how-to-import-jquery-and-mcustomscrollbar-plugin ...

  4. Linux学习之socket编程(二)

    Linux学习之socket编程(二) 1.C/S模型——UDP UDP处理模型 由于UDP不需要维护连接,程序逻辑简单了很多,但是UDP协议是不可靠的,实际上有很多保证通讯可靠性的机制需要在应用层实 ...

  5. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. Java实现断点续传。

    http://www.cnblogs.com/liaojie970/p/5013790.html

  7. 百度API调用实例

    今天依据需求要从百度API中取出一些数据.这些操作包含:将坐标转换成百度坐标.依据转换的百度坐标进行特定的查询. 有需求的收藏下,免得下次手写浪费时间. 涉及到的操作有:JSON格式的字符解析.HTT ...

  8. iOS开发之软键盘使用小技巧

    在iOS开发过程中,有时候须要弹出软键盘进行输入,有时候又须要在某些情况下隐藏软键盘,以提高用户体验. 今天有几个关于软键盘的小技巧和大家分享. (1)仅仅弹出数字键盘 有某些需求中,要求用户仅仅能在 ...

  9. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel

    https://technet.microsoft.com/en-us/library/bb397374(v=exchg.80).aspx Install the IIS 6.0 Management ...

  10. 客户端本地存储(cookie、web Storage、vuex)选择

    一.cookie .localStorage .sessionStorage .vuex 比较 cookie   4K    有时效性    可服务器传递 cookie是由服务器产生,存储在客户端的一 ...