如何在eclipse中开发web service (by quqi99)
2008-04-18 10:11
176 查看
如何在eclipse中开发web service (by quqi99)
如何在
eclipse
中开发
web service
1、
新建一个
WTP
动态
WEB
工程。
2、
在工程中新建一个类
,
如下:
package
zh;
/**
**
@
作者
张华
**
@
时间
2008
-
1
-
18
上午
09:43:34
**
@
描述
**/
public
class
HelloWold {
public
String
sayHello(){
return
"hello"
;
}
}
3、
如果要将刚建的
HelloWold
这个类的
sayHello
()方法发布的话。在左边
Package Explore
中的这个类上点右键,如下图:
默认完成,
WEB
服务就算建完了,简单吧。
4、
打开刚生成的
WebContent/wsdl/Hellowold.wsdl
文件
http://localhost:8083/test2/services/HelloWold
在
IE
中打开上述地址就能访问。
http://localhost:8083/test2/services/HelloWold?wsdl
便是我们的发布文件。
5、
当别家公司需要写
WEB
服务的客户端时,你只需要将地址:
http://localhost:8083/test2/services/HelloWold?wsdl
给他,他从
IE
访问这个地址,并另存为在随便一种什么类型的工程下,
6、
然后再对着这个文件点右键,便能自动生成客户端程序,如下图:
简单吧,
7、
下个测试方法测试一哈。
HelloWold hw=
new
HelloWoldProxy();
String result
=
null
;
try
{
result
=hw.sayHello();
}
catch
(RemoteException e) {
e.printStackTrace();
}
System.
out
.println(result
);
另外,axis2的方式,不过感觉这次生成出来的客户端没有上面好:
Version:1.0
StartHTML:0000000167
EndHTML:0000026378
StartFragment:0000001026
EndFragment:0000026362
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
H1 { margin-bottom: 0.21cm }
H1.western { font-family: "Arial", sans-serif; font-size: 16pt }
H1.cjk { font-family: "Arial Unicode MS"; font-size: 16pt; font-style: normal; font-weight: bold }
H1.ctl { font-family: "Tahoma"; font-size: 16pt; font-weight: bold }
H2 { margin-bottom: 0.21cm }
H2.western { font-family: "Arial", sans-serif; font-size: 14pt; font-style: italic }
H2.cjk { font-family: "Arial Unicode MS"; font-size: 14pt; font-style: italic }
H2.ctl { font-family: "Tahoma"; font-size: 14pt; font-style: italic }
A:link { so-language: zxx }
-->
6 axis2
6.1
在/home/soft/axis2-1.5.1/webapp
处ant
生成 /home/soft/axis2-1.5.1/dist/axis2.war
把axis2.war
文件复制到tomcat
的webapps
目录下即可完成Axis2
的安装,并访问:http://localhost:8080/axis2
下载Eclipse
Axis2
插件:axis2-eclipse-codegen-wizard-1.4.zip
和axis2-eclipse-codegen-wizard-1.4.zip
,安装好后,在新建向导中会有”Axis2
Wizards”
。
在
Eclispse
中添加一个用户库命名为
axis2
,将
axis2/lib
下的包均添加进来。这一步操作的目的是为了以后工程添加
axis2
所需的
jar
文件方便
随便建一个测试工程ElongDealsIntegrate
,将上步建立的库加入。
6.2
建类Hello.java
package
quqi;
public
class
Hello {
public
String sayHello(String user) {
return
"Hello, "
+ user;
}
}
2
)
打包要发布的
Service
,
Eclipse
中
New
--> File --> Other --> Axis2 wizards --> Axis2 Services
Archiver
,按照向导选择刚建立的类编译后的
class
文件。选择
class
文件目录,注意,不是
java
源文件,而是
classes
目录,这里需要注意由于你的类是带包quqi
的,因此不要选到包这个目录。如:/home/workspace/ElongDealsIntegrate/bin
3
)连按两次
“Next>”
,选中 “Generate
the service xml automatically”
4
)按下一步,输入
service
名称和类名,我这里输入的服务名是HelloService
;类名是我们刚刚写的类名:quqi.Hello
,这里需要注意加入完整的包名。
5)
按下一步,输入 service
文件的保存路径和文件名,完成。
我的选择生成目录为:/home/soft/tomcat5.5.23/webapps/axis2/WEB-INF/services
6)
这时候,你访问http://localhost:8080/axis2/services/listServices
就可以看到你刚建的HelloService
服务.
6.3
package
quqi;
import
javax.xml.namespace.QName;
import
org.apache.axis2.AxisFault;
import
org.apache.axis2.addressing.EndpointReference;
import
org.apache.axis2.client.Options;
import
org.apache.axis2.rpc.client.RPCServiceClient;
public
class
HelloClient {
private
RPCServiceClient
serviceClient
;
private
Options
options
;
private
EndpointReference
targetEPR
;
public
HelloClient(String endpoint)
throws
AxisFault {
serviceClient
=
new
RPCServiceClient();
options
=
serviceClient
.getOptions();
targetEPR
=
new
EndpointReference(endpoint);
options
.setTo(
targetEPR
);
}
public
Object[] invokeOp(String targetNamespace, String opName,
Object[]
opArgs, Class<?>[] opReturnType)
throws
AxisFault, ClassNotFoundException {
//
设定操作的名称
QName
opQName =
new
QName(targetNamespace, opName);
//
设定返回值
//
Class<?>[] opReturn = new Class[] { opReturnType };
//
操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return
serviceClient
.invokeBlocking(opQName,
opArgs, opReturnType);
}
public
static
void
main(String[] args)
throws
AxisFault,ClassNotFoundException {
final
String endPointReference =
"http://localhost:8080/axis2/services/HelloService"
;
final
String targetNamespace =
"http://quqi"
;
//
名空间,参见
http://localhost:8080/axis2/services/HelloService?wsdl
HelloClient
client =
new
HelloClient(endPointReference);
String
opName =
"sayHello"
;
Object[]
opArgs =
new
Object[] {
"zhang"
};
Class<?>[]
opReturnType =
new
Class[] { String[].
class
};
Object[]
response = client.invokeOp(targetNamespace, opName,
opArgs,opReturnType);
System.
out
.println(((String[])
response[0])[0]);
}
}
6.4
Axis2 Codegen
Wizard
插件好像有问题,报
java.lang.reflect.InvocationTargetException,
下面我们将换一种方式生成客户端,即用axis2
自己提供的命令。
命令:/home/soft/axis2-1.5.1/bin/wsdl2java.sh
-uri http://localhost:8080/axis2/services/HelloService?wsdl -p
client.hello -d adb -s -o /home/workspace/ElongDealsIntegrate
/home/soft/axis2-1.5.1/bin/wsdl2java.sh
-uri http://211.151.230.209/NewNorthBoundService/V1.1/NorthBoundAPIService.asmx?wsdl -p com.elong.northboundapi -d adb -s -o
/home/workspace/ElongDealsIntegrate
这样会自动生成/home/workspace/ElongDealsIntegrate/src/client/hello/HelloServiceStub.java
调用:
package
quqi;
import
java.rmi.RemoteException;
import
client.hello.HelloServiceStub;
/**
*
@author
huazhang
*
*/
public
class
HelloADBClient {
public
static
void
main(String[] args)
throws
RemoteException
{
HelloServiceStub
stub =
new
HelloServiceStub(
"http://localhost:8080/axis2/services/HelloService"
);
sayHello
(stub);
}
private
static
void
sayHello(HelloServiceStub stub)
throws
RemoteException {
//
请求数据
HelloServiceStub.SayHello
req =
new
HelloServiceStub.SayHello();
req.setUser(
"zhang
hua"
);
//
响应结果
HelloServiceStub.SayHelloResponse
res = stub.sayHello(req);
System.
err
.println(res.get_return());
}
}
另外, 可通过TcpMon 及 soapUI 来观察soap情况.
如何在
eclipse
中开发
web service
1、
新建一个
WTP
动态
WEB
工程。
2、
在工程中新建一个类
,
如下:
package
zh;
/**
**
@
作者
张华
**
@
时间
2008
-
1
-
18
上午
09:43:34
**
@
描述
**/
public
class
HelloWold {
public
String
sayHello(){
return
"hello"
;
}
}
3、
如果要将刚建的
HelloWold
这个类的
sayHello
()方法发布的话。在左边
Package Explore
中的这个类上点右键,如下图:
默认完成,
WEB
服务就算建完了,简单吧。
4、
打开刚生成的
WebContent/wsdl/Hellowold.wsdl
文件
http://localhost:8083/test2/services/HelloWold
在
IE
中打开上述地址就能访问。
http://localhost:8083/test2/services/HelloWold?wsdl
便是我们的发布文件。
5、
当别家公司需要写
WEB
服务的客户端时,你只需要将地址:
http://localhost:8083/test2/services/HelloWold?wsdl
给他,他从
IE
访问这个地址,并另存为在随便一种什么类型的工程下,
6、
然后再对着这个文件点右键,便能自动生成客户端程序,如下图:
简单吧,
7、
下个测试方法测试一哈。
HelloWold hw=
new
HelloWoldProxy();
String result
=
null
;
try
{
result
=hw.sayHello();
}
catch
(RemoteException e) {
e.printStackTrace();
}
System.
out
.println(result
);
另外,axis2的方式,不过感觉这次生成出来的客户端没有上面好:
Version:1.0
StartHTML:0000000167
EndHTML:0000026378
StartFragment:0000001026
EndFragment:0000026362
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
H1 { margin-bottom: 0.21cm }
H1.western { font-family: "Arial", sans-serif; font-size: 16pt }
H1.cjk { font-family: "Arial Unicode MS"; font-size: 16pt; font-style: normal; font-weight: bold }
H1.ctl { font-family: "Tahoma"; font-size: 16pt; font-weight: bold }
H2 { margin-bottom: 0.21cm }
H2.western { font-family: "Arial", sans-serif; font-size: 14pt; font-style: italic }
H2.cjk { font-family: "Arial Unicode MS"; font-size: 14pt; font-style: italic }
H2.ctl { font-family: "Tahoma"; font-size: 14pt; font-style: italic }
A:link { so-language: zxx }
-->
6 axis2
6.1
准备
在/home/soft/axis2-1.5.1/webapp处ant
生成 /home/soft/axis2-1.5.1/dist/axis2.war
把axis2.war
文件复制到tomcat
的webapps
目录下即可完成Axis2
的安装,并访问:http://localhost:8080/axis2
下载Eclipse
Axis2
插件:axis2-eclipse-codegen-wizard-1.4.zip
和axis2-eclipse-codegen-wizard-1.4.zip
,安装好后,在新建向导中会有”Axis2
Wizards”
。
在
Eclispse
中添加一个用户库命名为
axis2
,将
axis2/lib
下的包均添加进来。这一步操作的目的是为了以后工程添加
axis2
所需的
jar
文件方便
随便建一个测试工程ElongDealsIntegrate
,将上步建立的库加入。
6.2
建立Webservice
服务
建类Hello.javapackage
quqi;
public
class
Hello {
public
String sayHello(String user) {
return
"Hello, "
+ user;
}
}
2
)
打包要发布的
Service
,
Eclipse
中
New
--> File --> Other --> Axis2 wizards --> Axis2 Services
Archiver
,按照向导选择刚建立的类编译后的
class
文件。选择
class
文件目录,注意,不是
java
源文件,而是
classes
目录,这里需要注意由于你的类是带包quqi
的,因此不要选到包这个目录。如:/home/workspace/ElongDealsIntegrate/bin
3
)连按两次
“Next>”
,选中 “Generate
the service xml automatically”
4
)按下一步,输入
service
名称和类名,我这里输入的服务名是HelloService
;类名是我们刚刚写的类名:quqi.Hello
,这里需要注意加入完整的包名。
5)
按下一步,输入 service
文件的保存路径和文件名,完成。
我的选择生成目录为:/home/soft/tomcat5.5.23/webapps/axis2/WEB-INF/services
6)
这时候,你访问http://localhost:8080/axis2/services/listServices
就可以看到你刚建的HelloService
服务.
6.3
生成客户端之一(直接生成)
packagequqi;
import
javax.xml.namespace.QName;
import
org.apache.axis2.AxisFault;
import
org.apache.axis2.addressing.EndpointReference;
import
org.apache.axis2.client.Options;
import
org.apache.axis2.rpc.client.RPCServiceClient;
public
class
HelloClient {
private
RPCServiceClient
serviceClient
;
private
Options
options
;
private
EndpointReference
targetEPR
;
public
HelloClient(String endpoint)
throws
AxisFault {
serviceClient
=
new
RPCServiceClient();
options
=
serviceClient
.getOptions();
targetEPR
=
new
EndpointReference(endpoint);
options
.setTo(
targetEPR
);
}
public
Object[] invokeOp(String targetNamespace, String opName,
Object[]
opArgs, Class<?>[] opReturnType)
throws
AxisFault, ClassNotFoundException {
//
设定操作的名称
QName
opQName =
new
QName(targetNamespace, opName);
//
设定返回值
//
Class<?>[] opReturn = new Class[] { opReturnType };
//
操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return
serviceClient
.invokeBlocking(opQName,
opArgs, opReturnType);
}
public
static
void
main(String[] args)
throws
AxisFault,ClassNotFoundException {
final
String endPointReference =
"http://localhost:8080/axis2/services/HelloService"
;
final
String targetNamespace =
"http://quqi"
;
//
名空间,参见
http://localhost:8080/axis2/services/HelloService?wsdl
HelloClient
client =
new
HelloClient(endPointReference);
String
opName =
"sayHello"
;
Object[]
opArgs =
new
Object[] {
"zhang"
};
Class<?>[]
opReturnType =
new
Class[] { String[].
class
};
Object[]
response = client.invokeOp(targetNamespace, opName,
opArgs,opReturnType);
System.
out
.println(((String[])
response[0])[0]);
}
}
6.4
生成客户端之二(根据wsdl
由工具自动生成)
Axis2 CodegenWizard
插件好像有问题,报
java.lang.reflect.InvocationTargetException,
下面我们将换一种方式生成客户端,即用axis2
自己提供的命令。
命令:/home/soft/axis2-1.5.1/bin/wsdl2java.sh
-uri http://localhost:8080/axis2/services/HelloService?wsdl -p
client.hello -d adb -s -o /home/workspace/ElongDealsIntegrate
/home/soft/axis2-1.5.1/bin/wsdl2java.sh
-uri http://211.151.230.209/NewNorthBoundService/V1.1/NorthBoundAPIService.asmx?wsdl -p com.elong.northboundapi -d adb -s -o
/home/workspace/ElongDealsIntegrate
这样会自动生成/home/workspace/ElongDealsIntegrate/src/client/hello/HelloServiceStub.java
调用:
package
quqi;
import
java.rmi.RemoteException;
import
client.hello.HelloServiceStub;
/**
*
@author
huazhang
*
*/
public
class
HelloADBClient {
public
static
void
main(String[] args)
throws
RemoteException
{
HelloServiceStub
stub =
new
HelloServiceStub(
"http://localhost:8080/axis2/services/HelloService"
);
sayHello
(stub);
}
private
static
void
sayHello(HelloServiceStub stub)
throws
RemoteException {
//
请求数据
HelloServiceStub.SayHello
req =
new
HelloServiceStub.SayHello();
req.setUser(
"zhang
hua"
);
//
响应结果
HelloServiceStub.SayHelloResponse
res = stub.sayHello(req);
System.
err
.println(res.get_return());
}
}
另外, 可通过TcpMon 及 soapUI 来观察soap情况.
相关文章推荐
- CentOS6.5下如何正确下载、安装Intellij IDEA、Scala、Scala-intellij-bin插件、Scala IDE for Eclipse助推大数据开发(图文详解)
- 如何在eclipse开发环境中连接数据库?oracle和db2
- 如何将MyEclipse开发的项目导入到Eclipse中运行
- 如何在windows7下建立Pixhawk的Eclipse编译开发环境
- 如何在各种开发语言中调用web service
- java开发环境搭建---如何给eclipse添加中文包(三)
- ]Axis2与Eclipse整合开发Web Service:WSDL逆向生成服务端
- Axis2与Eclipse整合开发Web Service:简单的计算服务例子
- ubuntu14.04如何在线安装eclipse以及C/C++开发组件,搭建软件开发平台
- [转]如何在Linux使用Eclipse + CDT开发C/C++程序? (OS) (Linux) (C/C++) (gcc) (g++)
- 如何使用eclipse进行嵌入式Linux的开发
- [图解教程] Axis2与Eclipse整合开发Web Service之一:简单的计算服务例子
- 如何提高eclipse开发效率
- 千里之路—— Eclipse开发JAVA,如何新建java项目?
- Eclipse开发Android程序如何在手机上运行
- 如何将MyEclipse开发的项目导入到Eclipse中运行
- 如何将MyEclipse开发的项目导入到Eclipse中运行
- 如何配置Java EE Eclipse+Tomcat开发环境
- 安卓开发——如何查看Androidstudio和Eclipse导出的APK文件
- 如何用eclipse开发有c++11新特性的C++程序