现在开发一般都会用主流的架构SSH,当然有时候在我们所要实现的功能中需要调用别人开发的功能,比方说查询天气情况,而此时就需要用SSH去调用查询天气的webService服务了。所以webService和SSH之间的整合是必然的。当然我们可以调用别人发布的webService,也可以自己发布webService功能以供别人调用。在这节中我们先来说说如何发布webService吧。其实很简单的,也就是以下几个步骤:
1、将cxf和spring相关jar包考入到lib下面,大致有下面这些(红框是spring的,外面的是cxf的):
2、编写webService接口及代码
1)接口
package
com.tgb.web.webservice;
import javax.jws.WebService;@WebServicepublic interface HelloWorld {public String sayHi(String str);}
2)实现类:
package
com.tgb.web.webservice.impl;
3、在web.xml配置文件中加入spring和cxf的配置import java.util.Date;import javax.jws.WebService;import com.tgb.web.webservice.HelloWorld;@WebService(endpointInterface="com.tgb.web.webservice.HelloWorld",serviceName="HelloWorld")public class HelloWorldBean implements HelloWorld {@Overridepublic String sayHi(String str) {return "hello,"+str+",现在时间是:"+new Date();}}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 在Web Server启动的时候将BeanFactory放到ServletContext中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF配置 -->
<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>/cxfservice/*</url-pattern>
</servlet-mapping>
</web-app>
4、在applicationContext.xml配置文件中加入cxf的scahme
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 导入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="helloService" class="com.tgb.web.webservice.impl.HelloWorldWs">
</bean>
<!-- implementor指定webService的服务提供者。支持两种形式
A:直接给定服务器提供者的类名
B:设置为容器中一个Bean,采用注入方式
-->
<jaxws:endpoint implementor="#helloService" address="/fkjava" >
<!-- 配置in拦截器
<jaxws:inInterceptors>
<ref bean="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<ref bean="com.tgb.web.util.AuthInterceptor"/>
</jaxws:inInterceptors> -->
<!-- 配置out拦截器
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor"/>
</jaxws:outInterceptors> -->
</jaxws:endpoint>
</beans>
5、将web程序发布到服务器(如tomcat)上,后访问地址: http://***:8080/cxf_spring/fkservice/fkjava?wsdl。客户端则和以前的一样,再此不在重复