axis入门之代码篇(1)
jacky于2006广州配置好了环境,我们就可以进行编码了.
一: 建立一个普通的java类做我们的server
1
package
com.test.service;
2
3 public class TestServer {
4 public String say(String arg) {
5 System.out.println("some one call me!");
6 return arg;
7 }
8}
这个类定义一个say()的方法提供给客户端调用,返回类型是String.
2
3 public class TestServer {
4 public String say(String arg) {
5 System.out.println("some one call me!");
6 return arg;
7 }
8}
ok,这个类是最简单不过了.
下面建立我们的部署文件:WEB-INF/server-config.wsdd
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 < deployment xmlns ="http://xml.apache.org/axis/wsdd/" xmlns:java ="http://xml.apache.org/axis/wsdd/providers/java" >
3 < globalConfiguration >
4 < parameter name ="adminPassword" value ="admin" />
5 < parameter name ="attachments.Directory" value ="C:\eclipse\workspace\bookstore\WEB-INF\attachments" />
6 < parameter name ="attachments.implementation" value ="org.apache.axis.attachments.AttachmentsImpl" />
7 < parameter name ="sendXsiTypes" value ="true" />
8 < parameter name ="sendMultiRefs" value ="true" />
9 < parameter name ="sendXMLDeclaration" value ="true" />
10 < parameter name ="axis.sendMinimizedElements" value ="true" />
11 < requestFlow >
12 < handler type ="java:org.apache.axis.handlers.JWSHandler" >
13 < parameter name ="scope" value ="session" />
14 </ handler >
15 < handler type ="java:org.apache.axis.handlers.JWSHandler" >
16 < parameter name ="scope" value ="request" />
17 < parameter name ="extension" value =".jwr" />
18 </ handler >
19 </ requestFlow >
20 </ globalConfiguration >
21 < handler name ="LocalResponder" type ="java:org.apache.axis.transport.local.LocalResponder" />
22 < handler name ="Authenticate" type ="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
23 < handler name ="URLMapper" type ="java:org.apache.axis.handlers.http.URLMapper" />
24 < service name ="Version" provider ="java:RPC" >
25 < parameter name ="allowedMethods" value ="getVersion" />
26 < parameter name ="className" value ="org.apache.axis.Version" />
27 </ service >
28 < service name ="TestServer" provider ="java:RPC" >
29 < parameter name ="className" value ="com.test.service.TestServer" />
30 < parameter name ="allowedMethods" value ="*" />
31
32 </ service >
33 < service name ="AdminService" provider ="java:MSG" >
34 < parameter name ="allowedMethods" value ="AdminService" />
35 < parameter name ="enableRemoteAdmin" value ="false" />
36 < parameter name ="className" value ="org.apache.axis.utils.Admin" />
37 < namespace > http://xml.apache.org/axis/wsdd/ </ namespace >
38 </ service >
39 < transport name ="local" >
40 < responseFlow >
41 < handler type ="LocalResponder" />
42 </ responseFlow >
43 </ transport >
44 < transport name ="http" >
45 < requestFlow >
46 < handler type ="URLMapper" />
47 < handler type ="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
48 </ requestFlow >
49 </ transport >
50
51
52 </ deployment >
2 < deployment xmlns ="http://xml.apache.org/axis/wsdd/" xmlns:java ="http://xml.apache.org/axis/wsdd/providers/java" >
3 < globalConfiguration >
4 < parameter name ="adminPassword" value ="admin" />
5 < parameter name ="attachments.Directory" value ="C:\eclipse\workspace\bookstore\WEB-INF\attachments" />
6 < parameter name ="attachments.implementation" value ="org.apache.axis.attachments.AttachmentsImpl" />
7 < parameter name ="sendXsiTypes" value ="true" />
8 < parameter name ="sendMultiRefs" value ="true" />
9 < parameter name ="sendXMLDeclaration" value ="true" />
10 < parameter name ="axis.sendMinimizedElements" value ="true" />
11 < requestFlow >
12 < handler type ="java:org.apache.axis.handlers.JWSHandler" >
13 < parameter name ="scope" value ="session" />
14 </ handler >
15 < handler type ="java:org.apache.axis.handlers.JWSHandler" >
16 < parameter name ="scope" value ="request" />
17 < parameter name ="extension" value =".jwr" />
18 </ handler >
19 </ requestFlow >
20 </ globalConfiguration >
21 < handler name ="LocalResponder" type ="java:org.apache.axis.transport.local.LocalResponder" />
22 < handler name ="Authenticate" type ="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
23 < handler name ="URLMapper" type ="java:org.apache.axis.handlers.http.URLMapper" />
24 < service name ="Version" provider ="java:RPC" >
25 < parameter name ="allowedMethods" value ="getVersion" />
26 < parameter name ="className" value ="org.apache.axis.Version" />
27 </ service >
28 < service name ="TestServer" provider ="java:RPC" >
29 < parameter name ="className" value ="com.test.service.TestServer" />
30 < parameter name ="allowedMethods" value ="*" />
31
32 </ service >
33 < service name ="AdminService" provider ="java:MSG" >
34 < parameter name ="allowedMethods" value ="AdminService" />
35 < parameter name ="enableRemoteAdmin" value ="false" />
36 < parameter name ="className" value ="org.apache.axis.utils.Admin" />
37 < namespace > http://xml.apache.org/axis/wsdd/ </ namespace >
38 </ service >
39 < transport name ="local" >
40 < responseFlow >
41 < handler type ="LocalResponder" />
42 </ responseFlow >
43 </ transport >
44 < transport name ="http" >
45 < requestFlow >
46 < handler type ="URLMapper" />
47 < handler type ="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
48 </ requestFlow >
49 </ transport >
50
51
52 </ deployment >
初看一下,有点复杂,我们先不管其中具体的配置是什么作用,看一下我们关注的:
1
<
service
name
="TestServer"
provider
="java:RPC"
>
2 < parameter name ="className" value ="com.test.service.TestServer" />
3 < parameter name ="allowedMethods" value ="*" />
4 </ service >
这一段就是我们配置的service.name是我们给服务起的名称.下来的className就是我们写的类的路径.再往下就是允许调用的方法,这里是为全部.
2 < parameter name ="className" value ="com.test.service.TestServer" />
3 < parameter name ="allowedMethods" value ="*" />
4 </ service >
不算复杂吧!
现在,可以启动你的web服务器来看看建立的服务是否成功了.
看一下我们在web.xml里配置的这么一段:
1
<
servlet-mapping
>
2 < servlet-name > AxisServlet </ servlet-name >
3 < url-pattern > /services/* </ url-pattern >
4 </ servlet-mapping >
知道什么意思了吗,就是从/services/请求的地址都是我们的service.
2 < servlet-name > AxisServlet </ servlet-name >
3 < url-pattern > /services/* </ url-pattern >
4 </ servlet-mapping >
从浏览器中浏览:
http://localhost/WebService/services/
将会显示你现在所有的服务!那么表示我们建立的服务已经成功.
那下面就写一个客户端代码调用我们的service.
1
public
static
void
main(String [] args)
2 {
3 try {
4 Options options = new Options(args);
5
6 String endpointURL = "http://localhost/WebService/services/TestServer";//options.getURL();
7 String textToSend;
8
9 args = options.getRemainingArgs();
10
11 textToSend = "哈哈";
12
13 Service service = new Service();
14 Call call = (Call) service.createCall();
15
16 call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
17
18 //第一个参数是包名,也就是包名的倒叙(可有可无,因为上面的endpointURL已经指定了类),后面是方法名
19 QName q = new QName("", "say");
20 call.setOperationName( q );
21 call.addParameter( "arg1", XMLType.XSD_STRING, ParameterMode.IN);//传参数
22 call.setReturnType( XMLType.QNAME_FAULTACTOR);//设定返回参数
23
24 String ret = (String) call.invoke( new Object[] { textToSend } );
25
26 System.out.println("返回的东西 : " + ret );
27
28 } catch (Exception e) {
29 System.err.println(e.toString());
30 }
31 }
32 }
在你的web服务器还跑的时候,运行上面的代码.自己看看结果吧.
2 {
3 try {
4 Options options = new Options(args);
5
6 String endpointURL = "http://localhost/WebService/services/TestServer";//options.getURL();
7 String textToSend;
8
9 args = options.getRemainingArgs();
10
11 textToSend = "哈哈";
12
13 Service service = new Service();
14 Call call = (Call) service.createCall();
15
16 call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
17
18 //第一个参数是包名,也就是包名的倒叙(可有可无,因为上面的endpointURL已经指定了类),后面是方法名
19 QName q = new QName("", "say");
20 call.setOperationName( q );
21 call.addParameter( "arg1", XMLType.XSD_STRING, ParameterMode.IN);//传参数
22 call.setReturnType( XMLType.QNAME_FAULTACTOR);//设定返回参数
23
24 String ret = (String) call.invoke( new Object[] { textToSend } );
25
26 System.out.println("返回的东西 : " + ret );
27
28 } catch (Exception e) {
29 System.err.println(e.toString());
30 }
31 }
32 }
easy?