WebService说起来容易, 做起来还真有些让人一头雾水,先看了《JAVA WEB服务 构建与运行》和《JavaWebSererSOAP》大致了解了一下WebService,可是还是不得要领,想用Java做一个WebService服务的例
-
http://www.voidcn.com/article/p-rnvrmutt-wo.html
Axis2 Web服务配置文件services.xml详解
-
http://www.ibm.com/developerworks/cn/webservices/ws-apacheaxis2/ Web 服务与 Axis2 体系结构
-
http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/02/249556.html WebService大讲堂之Axis2
-
配置环境变量:AXIS2_HOME,PATH(JDK支持,具体方法略)
-
搭建Axis2服务环境需要Apach-tomcat支持,将axis2-1.6.0-war.zip解压后的.war文件部署到tomcat中。
-
启动tomcat,浏览器访问: http://localhost:8080/axis2/
-
看到Welcome,安装Axis2服务成功,欢迎页面中上的Services:服务列表,Validate:检验系统环境,Administration:Axis2安装管理控制台(登录的默认用户密码在:axis2\WEB-INF\conf\axis2.xml下)。
-
1
2
<
parameter
name
=
"userName"
>admin</
parameter
>
<
parameter
name
=
"password"
>axis2</
parameter
>
1 2 3 4 5 6 7 8 9
package
com.axis.service;
public
class
HelloService {
public
String sayHello(String name) {
if
(
null
== name) {
name =
"boby"
;
}
return
"Hello , "
+ name;
}
}
1
2
3
4
5
6
7
8
9
10
11
<
service
name
=
"HelloService"
scope
=
"application"
targetNamespace
=
"http://service.axis.com"
>
<
description
>
HelloService POJO Service
</
description
>
<
parameter
name
=
"ServiceClass"
>
com.axis.service.HelloService
</
parameter
>
<
operation
name
=
"sayHello"
>
<
messageReceiver
class
=
"org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
</
operation
>
</
service
>
1
2
3
<
ns:sayHelloResponse
xmlns:ns
=
"http://service.axis.com"
>
<
ns:return
>Hello , Jack</
ns:return
>
</
ns:sayHelloResponse
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package
com.axis.client;
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
HelloServiceClient {
private
static
String url1 =
"http://localhost:8080/axis2/services/HelloService"
;
public
static
void
main(String[] args)
throws
AxisFault {
RPCServiceClient client;
client =
new
RPCServiceClient();
Options options = client.getOptions();
System.out.println(
"======sayHello========"
);
EndpointReference endpoint =
new
EndpointReference(url1);
options.setTo(endpoint);
QName qname =
new
QName(
"http://service.axis.com"
,
"sayHello"
);
Object[] methodArgs =
new
Object[] {
"Tom"
};
@SuppressWarnings
(
"rawtypes"
)
Class[] returnType =
new
Class[] { String.
class
};
String result = (String) client.invokeBlocking(qname, methodArgs,
returnType)[
0
];
System.out.println(result);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<
serviceGroup
>
<
service
name
=
"MoneyService"
scope
=
"application"
>
<
description
>
MoneyService POJO Service
</
description
>
<
parameter
name
=
"ServiceClass"
>
com.axis.service.MoneyService
</
parameter
>
<
operation
name
=
"getMoney"
>
<
messageReceiver
class
=
"org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
</
operation
>
</
service
>
<
service
name
=
"WriteFileService"
scope
=
"application"
targetNamespace
=
"http://service.axis.com"
>
<
description
>
<
h3
>这是一个提供文件写入的服务,客户端将本地文件写入远程服务器</
h3
>
</
description
>
<
parameter
name
=
"ServiceClass"
>
com.axis.service.FileTransportImpl
</
parameter
>
<
messageReceivers
>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-out"
class
=
"org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-only"
class
=
"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"
/>
</
messageReceivers
>
</
service
>
</
serviceGroup
>