关闭

Axis2 Eclipse 开发webservice(3)

标签: webservice axis2httpclient4.3客户端代码soapUIHttpClient访问
773人阅读 评论(0) 收藏 举报
分类:

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>




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:131318次
    • 积分:2203
    • 等级:
    • 排名:第17206名
    • 原创:91篇
    • 转载:13篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论