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
原文地址:https://www.cnblogs.com/qitian1/p/6462651.html