Java HttpInvoker小试(转)

Httpinvoker是SpringFramework提供的远程通讯协议,只能用于JAVA程序间的通讯,且服务端和客户端必须使用SpringFramework。

 

package test;

import java.util.Map;

/**
 * @author zhuc
 * @version 2012-5-18 下午1:19:08
 */
public interface IService {
	/**
	 * @param msg
	 * @return
	 */
	public String getString(String msg);

	/**
	 * @param map
	 * @return
	 */
	public Map<String, Object> getMap(String map);
	
	/**
	 * @return
	 */
	public Car getCar();
}
package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhuc
 * @version 2012-5-18 下午1:19:32
 */
public class ServiceImpl implements IService {

	@Override
	public String getString(String msg) {
		return "hello : " + msg;
	}

	@Override
	public Map<String, Object> getMap(String map) {
		Map<String, Object> testMap = new HashMap<String, Object>();
		testMap.put("m1", 1);
		testMap.put("m2", "2");
		List<String> list = new ArrayList<String>();
		list.add("213");
		list.add("456");
		testMap.put("m3", list);
		testMap.put(map, map);
		System.out.println("远程调用:" + map);
		return testMap;
	}

	/* (non-Javadoc)
	 * @see test.IService#getCar()
	 */
	@Override
	public Car getCar() {
		Car c = new Car();
		c.setName("zhuc-httpinvoker-car");
		c.setCapacity(100);
		return c;
	}

}

 

package test;

import java.io.Serializable;

/**
 * @author zhuc
 * @version 2012-5-17 下午10:29:18
 */
public class Car implements Serializable {

	/** 
     *  
     */
	private static final long serialVersionUID = -1115598660168001267L;

	private String name;

	private transient Integer capacity;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the capacity
	 */
	public Integer getCapacity() {
		return capacity;
	}

	/**
	 * @param capacity the capacity to set
	 */
	public void setCapacity(Integer capacity) {
		this.capacity = capacity;
	}

}

 

 web.xml具体配置

 

remote-servlet.xml 如下:

  

 

 

客户端

 

package test;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author zhuc
 * @version 2012-5-18 下午1:22:39
 */
public class ClientTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"test/remote-client.xml");
		IService service = (IService) applicationContext
				.getBean("remoteService");
		String msg = service.getString("zhuc-httpinvoker");
		System.out.println(msg);

		Car c = service.getCar();
		System.out.println(c.getName());
		System.out.println(c.getCapacity());
		
		Map<String, Object> map = service.getMap("test");
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			System.out.println(entry.getKey() + "," + entry.getValue());
		}
	}

}

 

remote-client.xml

 

 

Properties代码 复制代码  收藏代码
  1. #url=http://localhost:8080/HttpInvoker/remote/remoteService   
  2. url=http://localhost:8080/HttpInvoker/remote/remote.convert.service  

 

   
 

 

输出结果如下:

hello : zhuc-httpinvoker
zhuc-httpinvoker-car
null
test,test
m1,1
m2,2
m3,[213, 456]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值