Axis2 Eclipse 开发webservice(3)

原创 2013年12月04日 11:05:27

Java编写webservice,通过axis2 发布,利用客户端代码,soapUI,HttpClient访问

原始java类

package com.yang.personservice;

import java.util.ArrayList;
import java.util.Iterator;

class Person{

	private int num;
	private String name;
	private String sex;
	
	public Person() {
		this.num = 000;
		this.name = "";
		this.sex = "";
	}
	
	public Person(int num, String name, String sex) {
		this.num = num;
		this.name = name;
		this.sex = sex;
	}
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Override
	public String toString() {
		return "Person num=" + num + ", name=" + name + ", sex=" + sex ;
	}
}

public class PersonTest {
	
	public static String queryPerson(int num, Iterator<Person> it) {
		String result = null;
		while(it.hasNext()){
			Person2 p = it.next();
			if(p.getNum() == num)
				return result = p.toString();
		}
		return result;
	}
	
	public static String queryPerson(String name, Iterator<Person> it) {
		String result = null;
		while(it.hasNext()){
			Person2 p = it.next();
			if(p.getName().equals(name))
				return result = p.toString();
		}
		return result;
	}
	public static void main(String[] args){
		
		ArrayList <Person> person = new ArrayList<Person>();
		person.add(new Person2(1001, "杨", "男"));
		person.add(new Person2(1002, "张三", "男"));
		person.add(new Person2(1003, "王五", "男"));
		person.add(new Person2(1004, "李四", "女"));
		person.add(new Person2(1005, "李四", "男"));
		
		System.out.println(queryPerson(1001, person.iterator()));
		System.out.println(queryPerson("杨", person.iterator()));		
		
	}
}
run as java application:

Person num=1001, name=杨飞, sex=男
Person num=1001, name=杨飞, sex=男

将上述源码简单修改,将其中的两个queryPerson函数修改为两个webservice方法,queryPersonByNum和queryPersonByName,如下所示


package com.yang.personservice;

import java.util.ArrayList;
import java.util.Iterator;

class Person {

	private int num;
	private String name;
	private String sex;
	
	public Person() {
		this.num = 000;
		this.name = "";
		this.sex = "";
	}
	
	public Person(int num, String name, String sex) {
		this.num = num;
		this.name = name;
		this.sex = sex;
	}
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Override
	public String toString() {
		return "Person num=" + num + ", name=" + name + ", sex=" + sex ;
	}
}

public class PersonService {
	
    private static ArrayList <Person> person = new ArrayList<Person>();

	public static void init(){		 
		person.add(new Person(1001, "杨", "男"));
		person.add(new Person(1002, "张三", "男"));
		person.add(new Person(1003, "王五", "男"));
		person.add(new Person(1004, "李四", "女"));
		person.add(new Person(1005, "李四", "男"));
	}
	
	public static String queryPersonByNum(int num) {
		init();
		String result = null;
		for (Iterator<Person> it = person.iterator(); it.hasNext();) {
			Person pp  = it.next();
			if (pp.getNum() == num) {
				return result = pp.toString();
			}
		}
		return result;
	}
	
	public static String queryPersonByName(String name) {
		init();
		String result = null;
		for (Iterator<Person> it = person.iterator(); it.hasNext();) {
			Person pp  = it.next();
			if (pp.getName().equals(name)) {
				return result = pp.toString();
			}
		}
		return result;
	}
}
利用axis2发布webservice,命名为personservice,参考http://blog.csdn.net/yang382197207/article/details/14124533

启动tomcat服务器,在浏览器查看personservice服务wsdl文件如下所示:


利用axis2生成客户端代码,参考:http://blog.csdn.net/yang382197207/article/details/14124533


编写测试类:

package client;

import java.rmi.RemoteException;

import com.yang.personservice.*;

public class PersonTest {

	public static void main(String[] args) throws RemoteException {
		// TODO Auto-generated method stub
		PersonServiceStub personstub = new PersonServiceStub();
		QueryPersonByNum query = new QueryPersonByNum();
		query.setNum(1001);
		QueryPersonByNumResponse resonpse = personstub.queryPersonByNum(query);
		System.out.println(resonpse.get_return());
		
		QueryPersonByName query2 = new QueryPersonByName();
		query2.setName("杨");
		QueryPersonByNameResponse resonpse2 = personstub.queryPersonByName(query2);
		System.out.println(resonpse2.get_return());
	}
}
确保web服务可以访问,执行上述程序:

Person num=1001, name=杨飞, sex=男

Person num=1001, name=杨飞, sex=男

利用soapUI访问刚刚部署的personservice服务

调用queryPersonByNum方法

调用queryPersonByName方法

编写HTTPclient访问服务,依赖包:httpclient-4.3.jar

测试代码:

package com.yang.httpcomponent.learntest;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class PersonServiceClientTest {


	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		// 服务地址
		String url = "http://localhost:8080/axis2/services/PersonService?wsdl";
		// 请求报文
		String soapRequestData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns=\"http://personservice.yang.com\">"
				+ "<soap:Header/>" 
				+ "<soap:Body>" 
				+ "<queryPersonByNum>"
				+ "<num>1001</num>" 
				+ "</queryPersonByNum>" 
				+ "</soap:Body>"
				+ "</soap:Envelope>";
		// 创建客户端
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// *****************get方法请求数据*********************
		// 创建httpget
		HttpGet httpget = new HttpGet(url);
		// 执行httpget
		CloseableHttpResponse getresponse = httpclient.execute(httpget);
		System.out.println("Get Response Result:" + getresponse.toString());
		// 获取消息实体,输出响应内容
		System.out.println("Get Response Entity********************** " );
		HttpEntity entity = getresponse.getEntity();
		if (entity != null) {
			String message = EntityUtils.toString(entity);
			System.out.println(message);
		}
		EntityUtils.consume(entity);
		// httpclient.close();

		// *****************post方法提交请求报文*********************
		System.out.println("************服务请求********************\n");
		// 创建httppost
		HttpPost httppost = new HttpPost(url);
		
		// 把Soap请求数据添加到httppost
		HttpEntity postrequest = new StringEntity(soapRequestData, HTTP.UTF_8);
		httppost.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
		httppost.setEntity(postrequest);
		
		//输出httppost请求报文
		System.out.println("SOAP Request********************** " );
		//System.out.println(EntityUtils.toString(postrequest));
		System.out.println(EntityUtils.toString(httppost.getEntity()));
		
		//执行post请求,输出响应结果
		System.out.println("SOAP Response ********************** " );
		CloseableHttpResponse postresponse = httpclient.execute(httppost);
		System.out.println(postresponse.getStatusLine());
		
		HttpEntity postresponseentity = postresponse.getEntity();
		String postresult = EntityUtils.toString(postresponseentity);
		System.out.println(postresult);
		
		EntityUtils.consume(postrequest);
		EntityUtils.consume(postresponseentity);
		
		httpclient.getConnectionManager().shutdown();  

	}
}

执行程序

SOAP Request********************** 
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns="http://personservice.yang.com"><soap:Header/><soap:Body><queryPersonByNum><num>1001</num></queryPersonByNum></soap:Body></soap:Envelope>
SOAP Response ********************** 
HTTP/1.1 200 OK
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><ns:queryPersonByNumResponse xmlns:ns="http://personservice.yang.com"><ns:return>Person num=1001, name=杨飞, sex=男</ns:return></ns:queryPersonByNumResponse></soapenv:Body></soapenv:Envelope>




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

eclipse+webservice(axis2)开发实例

1.参考文献: 1.利用Java编写简单的WebService实例  http://nopainnogain.iteye.com/blog/791525 2.Axis2与Eclipse整合开发Web ...

Apache axis2 + Eclipse 开发 WebService

简介 Apache Axis2是下一代 Apache Axis。Axis2 虽然由 Axis 1.x 处理程序模型提供支持,但它具有更强的灵活性并可扩展到新的体系结构。Axis2 基于新的体系结构进行...

Axis2 Eclipse 开发webservice(4)

写一个简单的java类,通过axis2发布为webservice,返回多个结果值,进行访问查询。 下面为java类 package yang.service.person; import java....

Axis2 Eclipse 开发webservice(1)

1、安装Eclipse和JDK,配置JDK的环境变量和Eclipse中JDK环境。 2、下载axis2,下载地址:http://axis.apache.org/axis2/java/core/dow...

Eclipse + Axis2 开发 WebService

在Eclipse中通过Axis2插件开发WebService服务端及客户端。

Axis2+tomcat6.0+eclipse开发webService

Axis2+tomcat6.0+eclipse开发webService 废话少说直接来干货! 首先下载Axis2并安装地址http://axis.apache.org/axis2/java/cor...

Apache axis2 + Eclipse 开发 WebService

一、简介 Apache Axis2是下一代 Apache Axis。Axis2 虽然由 Axis 1.x 处理程序模型提供支持,但它具有更强的灵活性并可扩展到新的体系结构。Axis2 基于新的体系结...

Apache axis2 + Eclipse 开发 WebService

参考这片文章: http://blog.csdn.net/xiaochunyong/article/details/7764683 文章很详细,只是最后出了点问题: ECLIPSE报错: Acc...

WebService从零到项目开发使用3—技术研究之Axis2 POJO开发Web服务

1.1.      Axis2 使用POJO开发Web服务 如果考虑快速开发一个Web服务,可以创建POJO(普通现有的java对象)然后使用axis2部署到tomcat上,这种方式容易快速开发、便...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)