參考網址:https://my.oschina.net/zimingforever/blog/212492
項目需要,這兩天系統要調一個webservice的服務,webservice的東西都扔了好幾年了,怎麼使用都忘得一幹二淨了。以前都是使用系統現成的架構掉一個方法就行了,現在幾乎是從0開始一點一點搭建環境啊
由于隻是調用服務,是以我這邊隻要實作一下spring環境下接入websevice就行了
第一中嘗試的是使用spring ws的WebServiceTemplate
配置的方法如下:
<bean id="xxxWebService" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri" value="http://XXX.net:8080"/> </bean>
第二種嘗試我用了jaxws
配置方法如下:
<bean id="xxxWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"> <property name="serviceInterface" value="com.xxx.UploadFileService"/> <property name="namespaceUri" value="http://XXX.net:8080"/> <property name="wsdlDocumentUrl" value="http://XXX.net:8080/XXXServicePort?WSDL" /> </bean>
第3中我嘗試的是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" />
<jaxws:client id="xxxWebService" serviceClass="com.xxx.UploadFileService"
address="http://XXX.net:8080">
</jaxws:client>
第4種我用的是xfire,也是最後項目采用的方法
<bean id ="dwdsspWebService" class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean"> <property name ="serviceClass"> <value>com.xxx.UploadFileService</value> </property> <property name ="wsdlDocumentUrl"> <value>http://XXX.net:8080/XXXServicePort?WSDL</value> </property> </bean>
參考文檔:http://blog.csdn.net/kkdelta/article/details/3987591
http://blog.csdn.net/kkdelta/article/details/7290769
http://blog.csdn.net/jadyer/article/details/9002984
http://blog.csdn.net/vickychen89/article/details/6606571
http://coach.iteye.com/blog/894159
第二種spring內建jaxws服務端
基于Spring IoC容器釋出Web服務,能夠大大降低WebService實作過程,也能夠更好的與企業級應用進行整合,本文將和大家介紹如何基于Spring和JAX-WS釋出WebService。
我們首先需要擷取項目所依賴的Jar包,這個過程比較繁瑣,筆者采用Maven建構項目,使用Maven進行項目管理的好處是我們隻需要在pom.xml檔案中配置依賴項目坐标,Maven就會自動將所需要的Jar包下載下傳到本地倉庫。
1.建立一個Maven Web項目,在pom.xml中添加如下内容:
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>123456789101112131415123456789101112131415
2.建立Web服務接口和實作類,這個過程和前面兩篇文中相同。
HelloWorld.Java
package com.csdn.ws.recipe03;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface HelloWorld {
@WebMethod
public String sayHello(String name);
}1234567891012345678910
HelloWorldImpl.java
package com.csdn.ws.recipe03;import javax.jws.WebService;import org.springframework.stereotype.Component;@Component@WebService(serviceName = "HelloWorldService", endpointInterface = "com.csdn.ws.recipe03.HelloWorld")public class HelloWorldImpl implements HelloWorld {
public String sayHello(String name) { return "Hello," + name;
}
}12345678910111213141234567891011121314
不同的是在實作類中添加了注解@Component,該注解用于Spring查找元件。
3.在web.xml檔案中添加spring的監聽器配置:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener></web-app>1234567891011121312345678910111213
4.建立一個source folder,名為config,在config下建立beans.xml,用于spring bean的配置。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.xsd">
<context:component-scan base-package="com.csdn.ws.recipe03"></context:component-scan>
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8089/services/"/>
</bean></beans>12345678910111213141234567891011121314
context:component-scan标簽指定了查找元件的包名,SimpleJaxWsServiceExporter類的baseAddress屬性用于指定webservice的根路徑,完整的web服務位址=根路徑+WebService名稱。
5.完整的項目結構如下圖所示:
6.通過位址http://localhost:8089/services/HelloWorldService?wsdl檢視WSDL文檔。
說明WebService釋出成功。
第五種是axis2+spring內建
1、建立一個web project項目,最終工程目錄如下:
注意:本文隻注重webservice伺服器端的開發,是以com.ljq.client和com.ljq.test忽略不計
2、添加所需jar
3、接口HelloWorld
package com.ljq.service;
public interface HelloWorld {
public String greeting(String name);
public String print();
}
4、接口實作類HelloWorldBean
public class HelloWorldBean implements HelloWorld {
public String greeting(String name) {
return "你好 "+name;
}
public String print() {
return "我叫林計欽";
5、webservice類HelloWorldWebService
import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.i18n.Messages;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 可能出現Axis2 spring bean not found 或者 Spring applicationContext not found。
*
* 解決辦法:建構自己的ServiceObjectSupplier,實作接口ServiceObjectSupplier,同時也實作Spring的ApplicationContextAware接口
* @author Administrator
*/
public class HelloWorldWebService implements ServiceObjectSupplier,
ApplicationContextAware {
private static ApplicationContext ctx;
public Object getServiceObject(AxisService axisService) throws AxisFault {
Parameter springBeanName = axisService.getParameter("SpringBeanName");
String beanName = ((String) springBeanName.getValue()).trim();
if (beanName != null) {
if (ctx == null)
throw new AxisFault("applicationContext is NULL! ");
if (ctx.getBean(beanName) == null)
throw new AxisFault("Axis2 Can't find Spring Bean: " + beanName);
return ctx.getBean(beanName);
} else {
throw new AxisFault(Messages.getMessage("paramIsNotSpecified",
"SERVICE_SPRING_BEANNAME"));
}
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.ctx = ctx;
6、配置web.xml檔案
<?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">
<!-- 添加spring監聽器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加載spring的配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 注冊axis2的servlet -->
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7、在WEB-INF目錄下配置applicationContext.xml(不存在則自己建立)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="helloWorld" class="com.ljq.service.HelloWorldBean"></bean>
</beans>
8、在WEB-INF\services\axis\META-INF\目錄下配置services.xml(不存在則自己建立)