Java-Spring-WebService最基础的配置示例
很早很早之前,就初步学习了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最基础的配置示例的更多相关文章
- 在上已个Java Spring MVC项目基础上加MyBatis
代码目录: /Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC 1. 首先在resource目录加上jdbc.properties: d ...
- Spring dbcp连接池简单配置 示例
一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...
- Spring操作指南-IoC基础环境配置(基于注解自动装配)
项目源码:http://code.taobao.org/p/LearningJavaEE/src/LearningSpring001%20-%20Automatically%20wiring%20be ...
- Spring操作指南-IoC基础环境配置(基于注解手动装配)
Source: http://code.taobao.org/p/LearningJavaEE/src/LearningSpring002%20-%20Wiring%20beans%20with%20 ...
- Spring操作指南-IoC基础环境配置(基于XML)
- 将 Java Spring Framework 应用程序迁移到 Windows Azure
我们刚刚发布了一个新教程和示例代码,以阐述如何在Windows Azure中使用 Java 相关技术.在该指南中,我们提供了分步教程,说明如何将 Java Spring Framework 应用程序( ...
- 学习笔记_J2EE_SSM_01_spring+springMVC+Mybatis整合_XML配置示例
spring+springMVC+Mybatis整合_XML配置示例 1.概述 spring+springMVC+Mybatis整合 XML配置方式 1.1 测试环境说明 名称 版本 备注 操作系统 ...
- 【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
编写Java Spring Boot应用,通过配置logging.path路径把日志输出在指定的文件夹中. 第一步:通过VS Code创建一个空的Spring Boot项目 第二步:在applicat ...
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)
Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...
随机推荐
- numpy基础篇-简单入门教程2
import numpy as np Array 数组 print(np.zeros((2, 2))) # [[0. 0.] [0. 0.]] print(np.ones((2, 2))) # [[1 ...
- java音乐播放之IO流处理
这个类仅仅能一直播放.知道音乐结束. 比AudioCilp要好一点. import java.io.*; import javax.sound.sampled.*; public class Test ...
- 【Android实战】Socket消息通信
这篇博客的内容是基于http://my.oschina.net/fengcunhan/blog/178155进行改造的.所以须要先看完这篇博客,然后再来看以下的内容. 1.须要完毕的功能是直播间的so ...
- css3 边框阴影效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- canvas中遇到的理解问题
1.lineDashOffset ctx.lineDashOffset = number 描述: setLineDash 用于设置开始绘制虚线的偏移量. 数字的正负表示左右偏移. 2.createLi ...
- 威联通212P 在admin用户密码正确情况下仍然无法登录WEB页面解决办法
*登录 telnet 执行以下语句: [~] # cp /etc/default_config/passwd /mnt/HDA_ROOT/.config/passwd[~] # cp /etc/def ...
- Python正则表达式初识(四)
今天继续给大家分享Python正则表达式基础知识,主要给大家介绍一下特殊字符“{}”的用法,具体的教程如下. 特殊字符“{}”实质上也是一个限定词的用法,其限定前面字符所出现的次数,其常用的模式有三种 ...
- tensorflow学习之路---解决过拟合
''' 思路:1.调用数据集 2.定义用来实现神经元功能的函数(包括解决过拟合) 3.定义输入和输出的数据4.定义隐藏层(函数)和输出层(函数) 5.分析误差和优化数据(改变权重)6.执行神经网络 ' ...
- 【RHEL7/CentOS7网络配置】
目录 网卡配置文件 查网卡信息 测试网络是否正常 使用 nmtui 命令配置网络 使用 nm-connection-editor 工具配置网络 修改回6.x版本的网卡名 Rhel/CentOS网络配置 ...