`
zhy584520
  • 浏览: 183802 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

hessiankit与java service

    博客分类:
  • IOS
 
阅读更多

最近搞移动OA,数据交换我们使用的是hessian,下载hessian object-c实现版的hessianKit,下载地址:http://sourceforge.net/projects/hessiankit/

 

http://wiki.caucho.com/Hessian_-_Objective-C_2.0_Implementation

 

官方只有一个简单的demo,大家一看就明白啦.我这里就直接上代码,把服务端,客户端的代码贴出来.

 

服务端代码(需要引入hessian-4.0.7.jar,junit.4-7.jar):

hessian-4.0.7.jar下载地址:http://hessian.caucho.com/
1.定义DTO对象SysUser,需要序例化,代码如下:

package com.ysit.user.service;

import java.io.Serializable;

public class SysUser implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6040668457562095264L;
	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;
	}
	
	
}

 

2.需要定义一个服务接口(BasicAPI),代码如下:

package com.ysit.user.service;

public interface BasicAPI {
	public boolean login(SysUser user);
}

 3.实现服务接口:BasicService 需要继承HessianServlet

public class BasicService extends HessianServlet implements BasicAPI {

	private String word="welcome liudonggan.";
	private SysUser sysUser=new SysUser();
	
	public String getWord() {
		return word;
	}

	public void setWord(String word) {
		this.word = word;
	}

	@Override
	public boolean login(SysUser user) {
		// TODO Auto-generated method stub
		if(user.getUserName().equals("liudonggan")&&user.getPassword().equals("123"))
		{
			return true;
		}
		return false;
	}

}

 

 

4.配置servlet,找到web.xml.代码如下

 <servlet>
   <servlet-name>login</servlet-name>
   <servlet-class>com.ysit.user.service.BasicService</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

 

5.单元测试,需要加入junit.jar代码如下:

package com.ysit.user.service;

import java.net.MalformedURLException;

import junit.framework.TestCase;
import org.junit.Test;

import com.caucho.hessian.client.HessianProxyFactory;

public class HissianTest extends TestCase {

	@Test
	public final void testLogin()
	{
		String url = "http://localhost:8080/hessianDemo/login";

		HessianProxyFactory factory = new HessianProxyFactory();
		BasicAPI basic=null;
		try {
			basic = (BasicAPI) factory.create(BasicAPI.class, url);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SysUser s=new SysUser();
		s.setUserName("liudonggan");
		s.setPassword("123");
		boolean flag=(boolean)basic.login(s);
		assertEquals(true, flag);
		System.out.println("Test is ok? " + flag);
	}
	
	
}

 

 客户端代码:

1.协议SysUser代码如下

@protocol SysUser <NSObject>

@property (nonatomic,retain) NSString *userName;
@property (nonatomic,retain) NSString *password;

@end

 

2.服务协议BasicAPI代码如下:

#import <Foundation/Foundation.h>
@class SysUser;

@protocol BasicAPI <NSObject>
-(BOOL) login:(id<SysUser>) sysUser;
@end

 

3.测试连接代码如下:

        [CWHessianArchiver setClassName:@"com.ysit.user.service.SysUser" forProtocol:@protocol(SysUser)];
        
        NSURL* urlLogin = [NSURL URLWithString:URLLOGIN];
        id<BasicAPI> proxy= (id<BasicAPI>)[CWHessianConnection proxyWithURL:urlLogin protocol:@protocol(BasicAPI)];
               
        id<SysUser> user = (id<SysUser>)[CWValueObject valueObjectWithProtocol:@protocol(SysUser)];
        [user setUserName:userName];
        [user setPassword:password];
        BOOL flag=(BOOL)[proxy login:user];

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics