带有包名的webservice

上次和大家说过一个比较简单的webservice,那个webservice是没有包名的。但是在实际应用中包名是一定会有的。只有这样才能进行多人开发,这样做webservice的发布就多了一道工序,但这个其实也是比较简单的。这次的webservice就比上次写复杂一点。下面就来看看怎么做吧

1.写一个vo类,这个类在后面会被使用到

public class User {
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}
}

2.写一个service类

import java.util.*;

public class DifficultService {
	public int[] getOneArray(int i) { // 取得一个数组
		int array[] = new int[i];
		for (int k = 0; k < i; k++) {
			array[k] = new Random().nextInt(10);
		}
		return array;
	}

	public String[][] get2Array() {
		return new String[][] { { "中国", "上海" }, { "中国", "北京" },
				{ "中国", "美国", "英国" } };
	}

	public User getUser() {
		User u = new User();
		u.setUsername("陈瑞银");
		u.setPassword("123456");
		return u;
	}
}

3.为了操作方便,可以在F盘(或随便)建个“MyDemo”文件夹

4.在MyDemo里在建3个文件夹,分别是META-INF,vo,service,vo里面就放User的class文件,service就放DifficultService的class文件,META-INF里面放个services.xml.这个xml内容如下

<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements. See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership. The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License. You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied. See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
  -->

<service name="DifficultService">
    <description>
        Web Service
    </description>
    <parameter name="ServiceClass">
        service.DifficultService
    </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>

这里的service最好和service类一致,就像上面那样。


4.给资源打包这里建议使用aar包必须确保已经配置好了环境变量

在此之前必须确保已经配置好了环境变量,之后进入cmd,进入MyDemo所在目录,使用打包命令就可以了,截图如下

这里需要注意后面有一个空格和一个点,之后回车打包就完成了。

5.发布webservice,把打好的aar包复制到tomcat安装目录的webapps\axis2\WEB-INF\services下面,启动tomcat就可以了

6.查看webservice是否发布成功

在浏览器地址栏输入http://localhost:8080/axis2/services/listServices会出现如下界面




只要看到这个界面,就说明webservice已经发布成功了

7.写一个客户端程序来访问这个webservice

import javax.xml.namespace.QName;

import org.apache.axis2.*;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;



public class Client {
  public static void main(String args[]) throws Exception{
	  RPCServiceClient client=new  RPCServiceClient();
	  Options option=client.getOptions();
	  String s="http://localhost:8080/axis2/services/DifficultService";
	  EndpointReference ref=new EndpointReference(s);
	  option.setTo(ref);
	  QName qname=new QName("http://service","getOneArray");
	  Object results[]=client.invokeBlocking(qname, new Object[]{3}, new Class[]{int[].class});
	  int[] array=(int[])results[0];
	  for(int i=0;i<array.length;i++){
		  System.out.println(array[i]);
	  }
		    
	  System.out.println("===========================");
	  qname=new QName("http://service","get2Array");
	  results=client.invokeBlocking(qname, new Object[]{}, new Class[]{String[][].class});
	  String str[][]=(String[][])results[0];
	  for(int i=0;i<str.length;i++){
		  for(int k=0;k<str[i].length;k++){
			  System.out.println(str[i][k]);
		  }
		 
	  }
	  //qname=new QName("http://ws.apache.org/axis2","getUser");
	  qname=new QName("http://service","getUser");
	  results=client.invokeBlocking(qname, new Object[]{}, new Class[]{User.class});
	  User u=(User)results[0];
	  System.out.println(u);
  }
}

这样子就可以了,来看一下效果截图

到这里程序就写好了,如果上面有什么写的不清楚的可以给我留言。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值