最近项目需要建立向客户方已有系统推送类似于RSS信息的富文本信息,并推送信息待定,由于我们本身项目是C++的,客户系统是java的,又要满足到推送信息可定制可扩展,并必要时可web查看,思量之下采用gsoap轻量级webserver库开发。
现用经典的hello项目进行技术可行性验证,同时记录下来,供日后遗忘时参考。
1)资料准备:
下载:
gsoap_2.8.22.zip,axis2-1.7.5-bin.zip,apache-ant-1.9.7-bin.zip,jdk-8u65-windows-x64.exe,eclipse_cn.zip,vs2015.ent_chs.iso
安装:
安装eclipse,vs2015,JDK
将gsoap、axis2、apache-ant解压到指定目录
配置编译环境:
JAVA_HOME=C:\software\Java\jdk1.8.0_65
AXIS2_HOME=E:\tools\gsoap\axis2-1.7.5
ANT_HOME=E:\tools\gsoap\apache-ant-1.9.7
GSOAP_HOME=E:\tools\gsoap\gsoap-2.8\gsoap
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
将
;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%AXIS2_HOME%\bin;%ANT_HOME%\bin;%GSOAP_HOME%\bin\win32
加入PATH环境变量
2)编译实现
gsoap生成:
将gsoap-2.8\gsoap\samples\hello\hello.h拷贝到指定目录(本人建立E:\work\test\gsoap\hello目录)
建立hello.bat文件,编译命令:
E:\tools\gsoap\gsoap-2.8\gsoap\bin\win32\soapcpp2.exe -S -IE:\tools\gsoap\gsoap-2.8\gsoap\import hello.h
备注:
(我给出了详细地址,如果设置了环境变量可直接环境变量,可以:
%GSOAP_HOME%\bin\win32\soapcpp2.exe -S -I%GSOAP_HOME%\import hello.h
)
在命令行进入该目录(E:\work\test\gsoap\hello)运行该文件(hello.bat),将生成一系列文件
将E:\tools\gsoap\gsoap-2.8\gsoap目下的
stdsoap2.h、stdsoap2.cpp(或stdsoap2.c)
拷贝到当前目录(E:\work\test\gsoap\hello)
服务端开发:
打开vs2015,在该目录(E:\work\test\gsoap\hello)下建立server_gsoap工程,并添加Additional Include目录..\..
创建helloserver.cpp文件,加入
#include "soapH.h"
#include "h.nsmap"
#include "stdsoap2.h"
int http_get(struct soap *_soap)
{
soap_response(_soap,SOAP_HTML);
soap_send(_soap,"<HTML>Hello World For Gsoap To Java!</HTML>");
soap_end_send(_soap);
return SOAP_OK;
};
int main()
{
struct soap add_soap;
soap_init(&add_soap);
int m = soap_bind(&add_soap, NULL, 4567, 100);
if (m < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}
add_soap.fget=http_get;
add_soap.send_timeout=3;
add_soap.recv_timeout=3;
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for ( ; ; )
{
int s = soap_accept(&add_soap);
if (s < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
soap_serve(&add_soap);//该句说明该server的服务
soap_end(&add_soap);
}
return 0;
//return soap_serve(soap_new());
}
int h__hello(struct soap *soap, char *&s)
{
s = soap_strdup(soap, "Hello World For Gsoap To Java!");
return SOAP_OK;
}
将目录(E:\work\test\gsoap\hello)的
soapH.h、soapStub.h、stdsoap2.h、soapC.cpp、soapServer.cpp、stdsoap2.cpp
加入工程,编译,生成我们的服务
客户端开发:
在目录(E:\work\test\gsoap\hello)下建立子目录client_java,运行命令
wsdl2java -uri .\h.wsdl -p com.client -o .\client_java
会在client_java目录生成src包及build.xml
然后进入子目录client_java,运行命令
ant
将根据client_java生成build包
打开eclipse.exe,新建立java project,选择路径E:\work\test\gsoap\hello\client_java,加载工程文件
在Project->Properties->Java Build Path->Libraries中Add Library...,选择User Libraty,命名axis2,
路径设置E:\tools\gsoap\axis2-1.7.5\lib,并将该路径下的.jar添加进来。
建立/src/com/client/HelloClient.java文件,
内容类似:
package com.client;
// filename: HelloClient.java
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.client.ServiceStub;
public class HelloClient {
public static void main(String[] args) throws Exception {
ServiceStub stub = new ServiceStub("http://127.0.0.1:4567");
ServiceStub.Hello req = new ServiceStub.Hello();
ServiceStub.HelloResponse resp = null;
resp = stub.hello(req);
System.out.println("resp = "+resp.getParam1());
}
}
测试:
启动服务端,运行客户端
测试成功!