日期:2014-05-16  浏览次数:20527 次

国人:JSON-RPC之初识
相关链接
1 json-rpc-for-java
http://code.google.com/p/json-rpc-for-java/downloads/list
2 国人的json-rpc-for-java比老外的更好用
http://blog.csdn.net/jxpath/archive/2009/11/27/4888260.aspx
使用之后发现这个框架:JSON-RPC要注意的三个地方,其它还有什么就没有试出来了呀
1 JavaBean对象要用默认的无参构造函数,必须实现 java.io.Serializable序列化接口
2 JS方面: 必须注意:你的参数是String类型请在前后加',如:{username:'张三'}, 如是空值后台也会报错!
在传递对象请确保:参数类型的正确性,如你在age年龄中输入了非数字后台将报错!
3 通过在web.xml中注册的方式不能用!

更多精彩请参见官方示例:有更强大的级联调用
一 环境:XP+Myeclipse6.6+Tomcat7+JDK1.6
二 项目目录结构如下图

1 UserJSON.java
package jcore.jsonrpc.rpcobj;
//必须是jcore.jsonrpc.rpcobj包下才可以免注册
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jcore.jsonrpc.common.JsonRpcObject;
import jcore.jsonrpc.entity.User;

public class UserJSON extends JsonRpcObject  implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	
	/**
	 * 直接返回普通类型
	 * */
	public String helloJSONPRC(String userName){
		//可从getAllInputParms获取页面值
		//String userName = (String)this.getAllInputParms().get("userName");	
		return "Hello World "+userName +" !";	
	}
	
	/***
	 * 返回JavaBean类型
	 * */
	public User getUser(){
		return new User(1,"张三","123456",25);
	}
	
	public String saveUser(User user){
		System.out.println("id: "+user.getUserId() + ",name:" + user.getUserName() + ",password: " + user.getUserPassword() + ",age: " + user.getAge());
		return "保存用户成功!";
	}
	
	
	/**
	 * 返回List集合
	 * */
	public List<User> getUserAll(){
		
     	List<User> users = new ArrayList<User>();
     	users.add(new User(1,"张三","123456",25));
     	users.add(new User(2,"李逵","123123",20));
     	return users; 		
	}
	
	/**
	 * 返回Map集合
	 * */
	public Map<String,User> getUserMapAll(){
		
     	Map<String,User> maps = new HashMap<String,User>();
     	maps.put("1",new User(1,"张三","123456",25));
     	maps.put("2",new User(2,"李逵","123123",20));
     	return maps; 		
	}
}

2 User.java
package jcore.jsonrpc.entity;

public class User implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	private int userId;
	private String userName ;
	private String userPassword;
	private int age ;
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public User(int userId, String userName, String userPassword, int age) {
		super();
		this.userId = userId;
		this.userName = userName;
		this.userPassword = userPassword;
		this.age = age;
	}
	
	/**
	 * 必须注意:JSON-RPC必须要求你的JavaBean对象要用默认的无参构造函数
	 * */
	public User(){
		
	}
}

3 web.xml
<servlet>
		<servlet-name>JSONRPCServlet</servlet-name>
		<servlet-class>
			jcore.jsonrpc.servlet.JSONRPCServlet
		</servlet-class>		
		<load-on-startup>2</load-on-startup>		
	</servlet>
	<servlet-mapping>
		<servlet-name>JSONRPCServlet</servlet-name>
		<url-pattern>/JRPC</url-pattern>
	</servlet-mapping>

3 index.jsp
<%@ page language="java"  pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/