struts2+spring3+hibernate4+cxf2.7搭建webservice实例

5 篇文章 0 订阅
5 篇文章 0 订阅

源码下载地址:http://download.csdn.net/detail/biboheart/5628059

web项目中用到webservice,试着在ssh项目中加入cxf来搭建webservice,第一次用,不是很熟悉,就先把最简单的搭起来。在应用中再优化升级。如果有前辈觉得哪里做得不够好,请指点一二,不胜感激。

费话不多说,开始做事。ssh项目就不在这里说搭建过程了,有其它很多入门教程。有时间我的博客里也会去记录一下。

原来ssh中已经引入的包有(我的ssh项目用的是struts2+spring3+hibernate4):

antlr-2.7.7.jar;aopalliance-1.0.jar;asm-3.3.jar;asm-commons-3.3.jar;asm-tree-3.3.jar;aspectjweaver-1.5.3.jar;c3p0-0.9.1.jar;commons-dbcp-1.4.jar;

commons-fileupload-1.2.2.jar;commons-io-2.0.1.jar;commons-lang3-3.1.jar;commons-logging-1.1.1.jar;commons-logging-api-1.1.jar;commons-pool-1.6.jar;

dom4j-1.6.1.jar;freemarker-2.3.19.jar;hibernate-c3p0-4.2.2.Final.jar;hibernate-commons-annotations-4.0.2.Final.jar;hibernate-core-4.2.2.Final.jar;

hibernate-entitymanager-4.2.2.Final.jar;hibernate-jpa-2.0-api-1.0.1.Final.jar;jackson-annotations-2.2.0.jar;jackson-core-2.2.0.jar;jackson-databind-2.2.0.jar;

javassist-3.15.0-GA.jar;jboss-logging-3.1.0.GA.jar;jboss-transaction-api_1.1_spec-1.0.1.Final.jar;junit-4.11.jar;log4j-1.2.17.jar;mysql-connector-java-5.1.18-bin.jar;

ognl-3.0.6.jar;slf4j-api-1.7.5.jar;slf4j-log4j12-1.7.5.jar;spring-aop-3.2.3.RELEASE.jar;spring-beans-3.2.3.RELEASE.jar;spring-context-3.2.3.RELEASE.jar;

spring-core-3.2.3.RELEASE.jar;spring-expression-3.2.3.RELEASE.jar;spring-jdbc-3.2.3.RELEASE.jar;spring-orm-3.2.3.RELEASE.jar;spring-tx-3.2.3.RELEASE.jar;

spring-web-3.2.3.RELEASE.jar;struts2-core-2.3.14.1.jar;struts2-json-plugin-2.3.14.1.jar;struts2-spring-plugin-2.3.14.1.jar;xwork-core-2.3.14.1.jar

 

cxf2.7的包:

cxf-2.7.5.jar;

httpasyncclient-4.0-beta3.jar;

httpclient-4.2.1.jar;

httpcore-4.2.2.jar;

httpcore-nio-4.2.2.jar;

neethi-3.0.2.jar;

wsdl4j-1.6.3.jar;

xmlschema-core-2.0.3.jar;

加入包完成后,开始配置web.xml文件:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	metadata-complete="true" version="3.0">
	<display-name>webservicetest</display-name>
	<description>webservicetest</description>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- CXF -->
	<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>/webServices/*</url-pattern>
	</servlet-mapping>
	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>
	<!-- struts2 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext-*.xml</param-value>
	</context-param>
	<!-- log4j -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
</web-app>


创建一个接口,代码:

package org.fkit.webService;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
	String sayHello(String name);
}


 

创建实现类,代码:

package org.fkit.webService.impl;

import javax.jws.WebService;

import org.fkit.webService.HelloWorld;

@WebService(endpointInterface = "org.fkit.webService.HelloWorld",serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String name) {
		System.out.println("hello " + name);
		return "hello " + name;
	}

}

建好了类,需要装配到spring中,让spring来管理,applicationContext-commons.xml文件中加入如下代码:

xmlns:jaxws=http://cxf.apache.org/jaxws

 

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd

 

<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" /> 

 <jaxws:endpoint id="webServiceHelloWorld" address="/HelloWorld"   implementor="org.fkit.webService.impl.HelloWorldImpl"/>

本来是在下面代码中改成了橙色,结果发表后显示还是和其它的一样。算了,自己到里面去比对吧。不太会用这编辑器。

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
	<context:annotation-config />
	<context:component-scan base-package="org.fkit" />
	<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" />  
	<jaxws:endpoint id="webServiceHelloWorld"
		address="/HelloWorld"
		implementor="org.fkit.webService.impl.HelloWorldImpl"/>

	<!-- bean definitions here -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
		<property name="fileEncoding" value="utf-8" />
	</bean>
	<bean id="dataSource" destroy-method="close"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${connection.driver_class}" />
		<property name="jdbcUrl" value="${connection.url}" />
		<property name="user" value="${connection.username}" />
		<property name="password" value="${connection.password}" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="c3p0.min_size">${c3p0.min_size}</prop>
				<prop key="c3p0.max_size">${c3p0.max_size}</prop>
				<prop key="c3p0.max_statements">${c3p0.max_statements}</prop>
				<prop key="c3p0.timeout">${c3p0.timeout}</prop>
			</props>
		</property>
		<property name="configLocations">
			<list>
				<value>classpath:hibernate.cfg.xml</value>
			</list>
		</property>
	</bean>
	<!-- more bean definitions go here -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 事务处理 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" />
			<tx:method name="save*" />
			<tx:method name="update*" />
			<tx:method name="modify*" />
			<tx:method name="edit*" />
			<tx:method name="delete*" />
			<tx:method name="remove*" />
			<tx:method name="repair" />
			<tx:method name="deleteAndRepair" />

			<tx:method name="get*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="load*" read-only="true" />
			<tx:method name="search*" read-only="true" />
			<tx:method name="datagrid*" read-only="true" />
			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* org.fkit.service.impl.*.*(..))"
			id="transactionPointcut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
	</aop:config>
</beans>

还需要配置一下struts2,不然会报错,不能找到action,我的这个配置文件是给其它配置文件继承的,代码是为webservice配置的。

<package name="webServices" extends="vcs-base" namespace="/webServices">   

             <action name="*">       

                       <result>{1}</result>   

             </action>

</package>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
    <package name="vcs-base" extends="struts-default">
        <!-- 配置全局结果 -->
        <global-results>
   			<result name="index">/index.jsp</result>
		</global-results>
    </package>
    <package name="webServices" extends="vcs-base" namespace="/webServices">
	    <action name="*">
	        <result>{1}</result>
	    </action>
	</package>
</struts>

现在可以启动tomcat了,我的是tomcat7。启动完成后,访问http://localhost:8080/WebserviceTest/webServices/HelloWorld?wsdl

如果显示出wsdl的内容,说明配置完成。





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值