webservice与spring整合发布后台服务
2016-04-29 23:04
579 查看
现在开发一般都会用主流的架构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;
@WebService
public interface HelloWorld {
public String sayHi(String str);
}
2)实现类:
package com.tgb.web.webservice.impl;
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 {
@Override
public String sayHi(String str) {
return "hello,"+str+",现在时间是:"+new Date();
}
}
3、在web.xml配置文件中加入spring和cxf的配置
<?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。客户端则和以前的一样,再此不在重复
1、将cxf和spring相关jar包考入到lib下面,大致有下面这些(红框是spring的,外面的是cxf的):
2、编写webService接口及代码
1)接口
package com.tgb.web.webservice;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHi(String str);
}
2)实现类:
package com.tgb.web.webservice.impl;
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 {
@Override
public String sayHi(String str) {
return "hello,"+str+",现在时间是:"+new Date();
}
}
3、在web.xml配置文件中加入spring和cxf的配置
<?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。客户端则和以前的一样,再此不在重复
相关文章推荐
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- android之定时器AlarmManager
- GUI - Web前端开发框架
- Extjs4.0 最新最全视频教程
- MyEclipse Web Project转Eclipse Dynamic Web Project
- axis备忘
- 创业如何选择WEB开发语言
- Erlang实现的一个Web服务器代码实例
- 防止网页脚本病毒执行的方法-from web
- 自学成才的秘密:115个 web Develop 资源
- 使用批处理修改web打印设置笔记 适用于IE
- Apache Web让JSP“动”起来
- web下载的ActiveX控件自动更新
- 推荐六款WEB上传组件性能测试与比较第1/10页
- 关于三种主流WEB架构的思考
- Windows XP Service Pack 3 RC1 v.3244 winxp补丁3 提供下载
- Run As Service runassrv.exe 详细参数第1/2页
- 安装MySQL在最后的start service停住了解决方法