一.构建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服务
下一步如图:
再下一步如图:
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