关闭

Axis2 Eclipse 开发webservice(3)

标签: webservice axis2httpclient4.3客户端代码soapUIHttpClient访问
881人阅读 评论(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
查看评论

Eclipse使用Axis2,spring开发jax-ws(webservice)的配置讲解

Eclipse使用Axis2,Spring开发jax-ws(webservice)的配置讲解:    jax-ws是java web service的标准规范,jdk本身就自带了API。但是如果要部署到服务器上的话,还需要一款实现。目前实现有metro(jax-ws ri)...
  • baidu_18607183
  • baidu_18607183
  • 2016-06-04 14:10
  • 890

Eclipse+axis2+tomcat 开发WebService示例

Eclipse+axis2+tomcat 开发WebService示例           一.不能使用package关键字发布方法 1.     ...
  • qq_25980625
  • qq_25980625
  • 2016-11-21 15:37
  • 417

使用Eclipse+axis2一步一步发布webservice

1、下载axis2相关软件http://axis.apache.org/axis2/java/core/download.html 2、Java环境配置:JAVA_HOME、JRE_HONE、PATH路径配置 3、安装插件:将axis2-eclipse-codegen-plugin-1....
  • pilihaotian
  • pilihaotian
  • 2016-09-12 15:06
  • 1937

eclipse安装Axis2插件和简单的webservice发布

Axis2与CXF是现在很主流的WebService开发框架(java6也已经支持了),项目上还都是基本上用前两种做开发,今天记录一下我安装Axis2插件在eclipse中的安装和写一个简单的测试代码。 一、 eclipse的版本为Luna Release (4.4.0) Axis2的版本是1...
  • wangchangpen62
  • wangchangpen62
  • 2015-04-21 15:26
  • 23207

Webservice实践(四)基于AXIS2的服务端开发

AXSI2 也是Webservice服务开发的一个优良框架。
  • csolo
  • csolo
  • 2017-01-23 16:37
  • 1448

用AXIS2发布WebService的方法 使用eclipse插件生成服务端和客户端

一、Axis2的下载和安装      1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本:       可以下载如下三个zip包:    ...
  • u014695188
  • u014695188
  • 2016-03-13 12:55
  • 3922

Java EE+axis2搭建webservice服务(二)

2 方法二 2.1准备工作 (1)Java环境变量的配置,这里不做介绍; (2)安装插件:将axis2-eclipse-codegen-plugin-1.6.0和axis2-eclipse-service-plugin-1.6.0两个jar包放入eclipse目录下的plugins中,重新启动...
  • yuyinghua0302
  • yuyinghua0302
  • 2015-12-31 11:19
  • 998

Axis2开发webservice案例详解

最近公司在一个项目上与另外一家公司做接口集成,需要webservice来处理数据的传递,本来我用的CXF简单配置下就可以发布了(关于CXF发布webservice的过程,在后面的博客中也会给个案例,欢迎扔板儿砖!~_~),但是对方用的是axis,本来以为用wsimport解析下wsdl文件就可以生成...
  • zp357252539
  • zp357252539
  • 2016-05-15 11:07
  • 2902

webservice框架对比-----Axis、Axis2和CXF【企业中最常用的webservice发布方式是CXF】

文章来源:http://zhidao.baidu.com/link?url=fFO1fJ-Fx58PHD0pD9NaoCkfSt-bUUnsDbqaTJEwh0zlMgJbltk5EcLNInfK1wKFbo7fnEsTeuJZAQisWn9FhK 新一代的 Web Services 框架如 ...
  • buster2014
  • buster2014
  • 2015-07-17 11:32
  • 879

eclipse开发webservice-axis2/spring+maven

前言 本文将讲述使用eclipse发布axis2服务的环境准备到发布过程。 客户端访问和axis2的RPC访问 axis2发布到自己的maven+spring web项目中
  • bestcxx
  • bestcxx
  • 2016-12-26 23:17
  • 2676
    个人资料
    • 访问:156705次
    • 积分:2452
    • 等级:
    • 排名:第17747名
    • 原创:91篇
    • 转载:13篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论