溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Apache CXF實現(xiàn)的SOAP形式的webservices

發(fā)布時間:2020-07-24 07:47:09 來源:網(wǎng)絡(luò) 閱讀:1584 作者:pangfc 欄目:開發(fā)技術(shù)

前言:常用的web service有兩種形式,SOAP和RESTful。這里先說SOAP的實現(xiàn)步驟,RESTful的形式后面再說

一 建項目,導(dǎo)jar包

(1)項目結(jié)構(gòu)

Apache CXF實現(xiàn)的SOAP形式的webservices


(2)在eclipse中新建一個動態(tài)web項目“CXFDemo”,并導(dǎo)入cxf中的jar包(路徑:/WEB-INF/lib/)。當(dāng)然,可以去官網(wǎng)下載,也可以直接使用我用過的jar包,鏈接:http://pan.baidu.com/s/1pKyelAV

(3)配置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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>CXFDemo</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/classes/service-beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>


二 web service服務(wù)端類

(1)接口CXFService.java:

package cn.zifangsky;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface CXFService {
	@WebMethod
	public String sayHello(@WebParam(name="name") String name);
}

說明:
@WebService:標(biāo)記表示該接口是一個WebService服務(wù)
@WebMethod:標(biāo)記表示W(wǎng)ebService中的方法
@WebParam(name=”paramName”)表示方法中的參數(shù),name屬性限制了參數(shù)的名稱,若沒有指定該屬性,參數(shù)將會被重命名

(2)具體的實現(xiàn)類CXFServiceImpl.java:

package cn.zifangsky.impl;

import cn.zifangsky.CXFService;

public class CXFServiceImpl implements CXFService {
	
	public String sayHello(String name) {
		return "Hello," + name + "\n" + Test.getExtraMessage();
	}

}

注:Test.java類:

package cn.zifangsky.impl;

public class Test {
	public static String getExtraMessage(){
		return "測試";
	}
}

(3)配置service-beans.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:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	
	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature" />
	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	<jaxws:server id="sayHelloServices" serviceClass="cn.zifangsky.CXFService" address="/soap/sayHello" >
		<jaxws:serviceBean>
			<bean class="cn.zifangsky.impl.CXFServiceImpl" />
		</jaxws:serviceBean>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:features>
			<ref bean="loggingFeature" />
			<wsa:addressing xmlns:wsa="http://cxf.apache.org/ws/addressing" />
		</jaxws:features>
		
	</jaxws:server>
		
</beans>

說明:

i)3個bean配置的是日志,在這里把日志相關(guān)的配置刪掉也不影響web service服務(wù)的正常使用

ii)address指的是發(fā)布后這個服務(wù)的路徑問題,比如說我這里就是:http://localhost:8080/CXFDemo/soap/sayHello

(4)測試

發(fā)布這個項目到tomcat,運行之后的效果如下:

Apache CXF實現(xiàn)的SOAP形式的webservices

點擊那個鏈接,可以發(fā)現(xiàn)sayHello這個服務(wù)的地址是:http://localhost:8080/CXFDemo/soap/sayHello?wsdl

到此,這個web service的服務(wù)端已經(jīng)配置完成


三 web service客戶端配置

服務(wù)端提供服務(wù),而客戶端使用服務(wù)端提供的服務(wù),因此客戶端只需要調(diào)用服務(wù)端提供的服務(wù)就可以了

(1)客戶端的項目結(jié)構(gòu):

Apache CXF實現(xiàn)的SOAP形式的webservices

(2)同樣是新建一個動態(tài)web工程,然后引入cxf的jar包,同時需要把服務(wù)端的CXFService接口打包成一個jar包,然后導(dǎo)入到客戶端的lib中,鏈接:http://pan.baidu.com/s/1eQVWups

Apache CXF實現(xiàn)的SOAP形式的webservices

(3)客戶端測試類ClientDemo.java:

package cn.zifangsky.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.zifangsky.CXFService;

public class ClientDemo {
	public static void main(String args[]){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
		
		CXFService clientHello = (CXFService) context.getBean("clientHello");
		String response = clientHello.sayHello("javaee");
		context.close();
		
		System.out.println("Response: " + response);   
        System.exit(0);  
	} 
	
}

(4)客戶端配置文件client-beans.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:jaxws="http://cxf.apache.org/jaxws"   
       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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
         
    <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature" />
	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />      
    <jaxws:client id="clientHello" serviceClass="cn.zifangsky.CXFService" address="http://localhost:8080/CXFDemo/soap/sayHello?wsdl">
    	<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
		<jaxws:features>
			<ref bean="loggingFeature" />
			<wsa:addressing xmlns:wsa="http://cxf.apache.org/ws/addressing" />
		</jaxws:features>
    </jaxws:client>   
</beans>

說明:

這里的address需要填寫服務(wù)的wsdl絕對地址,因此這里就是:http://localhost:8080/CXFDemo/soap/sayHello?wsdl


(5)測試

直接使用“Java Application”運行ClientDemo,輸出如下:

---------------------------
ID: 1
Address: http://localhost:8080/CXFDemo/soap/sayHello?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Action xmlns="http://www.w3.org/2005/08/addressing">http://zifangsky.cn/CXFService/sayHello</Action><MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:0d855e41-d638-4525-a3cf-8055c40f8605</MessageID><To xmlns="http://www.w3.org/2005/08/addressing">http://localhost:8080/CXFDemo/soap/sayHello?wsdl</To><ReplyTo xmlns="http://www.w3.org/2005/08/addressing"><Address>http://www.w3.org/2005/08/addressing/anonymous</Address></ReplyTo></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://zifangsky.cn/"><name>javaee</name></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
三月 14, 2016 4:51:41 下午 org.apache.cxf.services.CXFServiceService.CXFServicePort.CXFService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Mon, 14 Mar 2016 08:51:41 GMT], Server=[Apache-Coyote/1.1], transfer-encoding=[chunked]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Action xmlns="http://www.w3.org/2005/08/addressing">http://zifangsky.cn/CXFService/sayHelloResponse</Action><MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:504f4c19-5df9-42bd-bb02-761da4932490</MessageID><To xmlns="http://www.w3.org/2005/08/addressing">http://www.w3.org/2005/08/addressing/anonymous</To><RelatesTo xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:0d855e41-d638-4525-a3cf-8055c40f8605</RelatesTo></soap:Header><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://zifangsky.cn/"><return>Hello,javaee
測試</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
三月 14, 2016 4:51:41 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@62a76581: startup date [Mon Mar 14 16:51:39 GMT+08:00 2016]; root of context hierarchy
Response: Hello,javaee
測試

可以發(fā)現(xiàn),最后成功輸出了

Response: Hello,javaee
測試

因為我們在客戶端中只引用了CXFService.java這個接口,但是卻輸出了預(yù)期的內(nèi)容,說明客戶端的確是調(diào)用了服務(wù)端的服務(wù)并成功返回信息的


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI