一.建構jax-ws服務端
1.在myeclipse8.x中建一個基于jax-ws的項目,項目名稱“jsServer”
2.建立一個包,包名為“wstestserver”
3.在包中添加名為“CHelloWorldManager”的類,具體代碼:
public class CHelloWorldManager implements IHelloWorldManager {
public long add(long a, long b)
{
return a + b;
}
public void sayHello()
{
System.out.println("hello!");
}
public String reverse(String str)
{
StringBuffer strBuf = new StringBuffer();
int nLen = str.length();
for(int i=nLen; i>0; i--)
{
strBuf.append(str.charAt(i-1));
}
return strBuf.toString();
}
}
當然,IHelloWorldManager接口是什麼樣的,通過實作類,也就知道了,這裡不列出。
4.在jsServer中添加web service服務
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiQ3chVEa0V3bT9CX5RXa2Fmcn9CXwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwVP9cmYox2RiVnVHFma5ckYzZ1RhZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DNzcDMzQDM2EDMyETMzEDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
下一步如圖:
再下一步如圖:
5.配置jsServer項目的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">
<servlet>
<description>
JAX-WS endpoint - CHelloWorldManagerService
</description>
<display-name>CHelloWorldManagerService</display-name>
<servlet-name>CHelloWorldManagerService</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CHelloWorldManagerService</servlet-name>
<url-pattern>/CHelloWorldManagerPort</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
</web-app>
6.配置jsServer項目的sun-jaxws.xml如下:
<?xml version = "1.0"?>
<endpoints version="2.0"
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint name="CHelloWorldManagerPort"
implementation="wstestserver.CHelloWorldManagerDelegate"
url-pattern="/CHelloWorldManagerPort">
</endpoint></endpoints>
7.啟動jsServer服務
8.在浏覽器中輸入位址,通路該web service,位址(當然每個人的服務啟動計算機不一樣,需要對改位址進行适當調整,以及要注意端口)是:
http://localhost:8081/jsServer/CHelloWorldManagerPort?wsdl
通路後就會出現下圖效果
二.建立基于gsoap的VS2005的web service用戶端
1.建立一個名為“testxxwe”的控制台工程
2.到官網下載下傳gsoap_2.7.16.zip
3.根據.wsdl檔案,生成vs2005所需的頭檔案,具體指令:
wsdl2h -o helloworldManagerClient.h http://localhost:8081/jsServer/CHelloWorldManagerPort?wsdl
在目前目錄下就會生成helloworldManagerClient.h檔案
4.根據helloworldManagerClient.h頭檔案生成相關存根、代理程式。具體指令:
soapcpp2 -C helloworldManagerClient.h(注意:一定要是大寫C,不是小寫c。大寫C以為着C++,小寫的意味着c語言)
在目前目錄下就會生成如下這些檔案
2013/11/20 11:22 451 CHelloWorldManagerPortBinding.add.req.xml
2013/11/20 11:22 446 CHelloWorldManagerPortBinding.add.res.xml
2013/11/20 11:22 544 CHelloWorldManagerPortBinding.nsmap
2013/11/20 11:22 438 CHelloWorldManagerPortBinding.reverse.req.xml
2013/11/20 11:22 453 CHelloWorldManagerPortBinding.reverse.res.xml
2013/11/20 11:22 421 CHelloWorldManagerPortBinding.sayHello.req.xml
2013/11/20 11:22 433 CHelloWorldManagerPortBinding.sayHello.res.xml
2013/11/20 10:49 25,107 helloworldManagerClient.h
2013/11/20 11:22 107,005 soapC.cpp
2013/11/20 11:22 2,654 soapCHelloWorldManagerPortBindingProxy.h
2013/11/20 11:22 6,328 soapClient.cpp
2013/11/20 11:22 698 soapClientLib.cpp
2013/11/20 11:22 36,235 soapH.h
2013/11/20 11:22 11,365 soapStub.h
2013/11/20 11:41 443,486 stdsoap2.cpp
2010/04/06 12:23 83,916 stdsoap2.h
16 個檔案 719,980 位元組
5.将除了soapClientLib.cpp檔案外的所有生成.h,.cpp,.nsmap檔案導入VS2005工程
6.對testxxwe.cpp檔案做如下修改,以測試jsServer的三個服務接口(add,Reverse,sayHello),代碼如下:
// testxxwe.cpp : 定義控制台應用程式的入口點。
//
#include "stdafx.h"
#include "..\gsoapHelloWorldManagerCliend\soapH.h"
#include "..\gsoapHelloWorldManagerCliend\soapCHelloWorldManagerPortBindingProxy.h"
const int REVERSE_STRING_MAX = 256;
void addTestExample();
void ReverseStringTest();
void SayHelloTest();
int _tmain(int argc, _TCHAR* argv[])
{
addTestExample();
ReverseStringTest();
SayHelloTest();
return 0;
}
//在伺服器端調用sayHello(),這樣伺服器端就會輸出"Hello"
void SayHelloTest()
{
while (1)
{
struct soap hellomanager;
CHelloWorldManagerPortBinding hellobind;
ns1__sayHello toSayHello;
ns1__sayHelloResponse responseSayHello;
int nResult = hellobind.__ns1__sayHello(&toSayHello, &responseSayHello);
if (0 == nResult)
{
printf("The sayHello success!");
}
else
{
printf("soap error , errcode = %d\n", nResult);
}
printf("continue:plese choose y or n");
char c='c';
scanf("%c", &c);
getchar();
printf("%c\n", c);
if ('y' != c)
{
break;
}
}
}
//調用伺服器端的int add(int a, int b);
void addTestExample()
{
struct soap hellomanager;
CHelloWorldManagerPortBinding hellobind;
ns1__add toAdd;
int a=0;
int b=0;
while(1)
{
scanf("%d", &a);
scanf("%d", &b);
toAdd.arg0 = a;
toAdd.arg1 = b;
ns1__addResponse responseAdd;
int nResult = hellobind.__ns1__add(&toAdd, &responseAdd);
if (0 == nResult)
{
printf("The add(%d,%d) = %d", a, b, responseAdd.return_);
}
else
{
printf("soap error , errcode = %d\n", nResult);
}
printf("continue:plese choose y or n");
char c='c';
getchar();
scanf("%c", &c);
printf("%c\n", c);
if ('y' != c)
{
break;
}
}
}
//調用伺服器端的String Reverse(char * string);
void ReverseStringTest()
{
char *p = new char[REVERSE_STRING_MAX];
memset(p, 0, REVERSE_STRING_MAX);
while(1)
{
struct soap hellomanager;
CHelloWorldManagerPortBinding hellobind;
ns1__reverse toReverse;
if(NULL != p)
{
gets_s(p, REVERSE_STRING_MAX);
printf("the input string is:%s\n", p);
toReverse.arg0 = p;
}
else
{
printf("error here 2013年11月20日15:41:25");
break;
}
ns1__reverseResponse responseReverse;
int nResult = hellobind.__ns1__reverse(&toReverse, &responseReverse);
if (0 == nResult)
{
printf("The reverse(%s)\n", responseReverse.return_);
}
else
{
printf("soap error , errcode = %d\n", nResult);
}
printf("continue:plese choose y or n");
char c='c';
getchar();
scanf("%c", &c);
printf("%c\n", c);
if ('y' != c)
{
break;
}
}
if (NULL != p)
{
delete []p;
p = NULL;
}
}
7.編譯運作該VS2005工程,就可以正式測試了,我這裡測試已經OK
三.如果你還想用java的用戶端對jsServer進行通路測試,可以繼續閱讀該文章
1.建立一個名為jsClient的java程式
2.在改工程下添加web service client,如下圖:
下下一步如下圖:
最後點選完成即可。
3.添加test包,以及包下添加test.java
4.在test.java中添加如下所示的代碼:
package test;
import wstestserver.CHelloWorldManagerDelegate;
import wstestserver.CHelloWorldManagerService;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CHelloWorldManagerDelegate hwDelegete = new CHelloWorldManagerService().getCHelloWorldManagerPort();
long n = hwDelegete.add(3,5);
hwDelegete.sayHello();
String str = hwDelegete.reverse("Hello world!");
System.out.println(str);
System.out.println(n);
}
}
5.向工程中添加jax-ws包
OK,完畢!測試運作即可。
紀念一下吧,完成時間2013年11月20日16:46:52