xml-rpc 2.0和3.0实例

xml-rpc 2.0介绍:
1、运行在不同操作系统、不同环境
2、使用http作为传输协议
3、xml作为传送信息的编码格式


xml-rpc是一种简单的,轻量级的通过HTTP协议进行RPC通信的规范。一个xml-rpc消息就是一个请求
体为xml的HTTP-POST请求,被调用的方法在服务器端执行并将执行结果以xml格式编码后返回。

 

xml-rpc 2.0实例:

服务端类

public class MyXmlRpcServer extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        XmlRpcServer xmlRpc=new XmlRpcServer();
        xmlRpc.addHandler("myHandler", new MyHandler());
        byte [] result=xmlRpc.execute(req.getInputStream());
        resp.setContentType("text/xml");
        resp.setContentLength(result.length);
        OutputStream outputStream=resp.getOutputStream();
        outputStream.write(result);
        outputStream.flush();
        outputStream.close();
    }   

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

   
}

配置

<servlet>
        <servlet-name>MyXmlRpcServer</servlet-name>
        <servlet-class>xiu.rpc.MyXmlRpcServer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyXmlRpcServer</servlet-name>
        <url-pattern>/MyXmlRpcServer</url-pattern>
    </servlet-mapping>

 

测试

public class MyXmlRpcClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            XmlRpcClient xmlrpc = new XmlRpcClient("http://127.0.0.1:8080/rpc/MyXmlRpcServer");
            Vector params = new Vector();
            params.addElement("Tom");
            String result = (String) xmlrpc.execute("myHandler.sayHello",params);
            System.out.println(result);
        } catch (MalformedURLException e) {
            System.out.println(e.toString());
        } catch (XmlRpcException e) {
            System.out.println(e.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

 

rpc 3.0 写servlet

实例:

public class HelloHandler {

    public String sayHello(String str){
        return str;
    }
}

 


public class Server extends HttpServlet {

    private XmlRpcServletServer xmlRpc;
   
    public void init(ServletConfig config)throws ServletException{
        super.init(config);
        xmlRpc=new XmlRpcServletServer();
        PropertyHandlerMapping phm=new PropertyHandlerMapping();
        try {
            phm.addHandler("helloHandler", HelloHandler.class);
            xmlRpc.setHandlerMapping(phm);
            XmlRpcServerConfigImpl serverConfig=(XmlRpcServerConfigImpl)xmlRpc.getConfig();
            serverConfig.setEnabledForExtensions(true);
            serverConfig.setContentLengthOptional(false);
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        xmlRpc.execute(req, resp);
    } 
}

public static void main(String[] args) {
        XmlRpcClientConfigImpl config=new XmlRpcClientConfigImpl();
        try {
            config.setServerURL(new URL("http://127.0.0.1:8080/rpc3/xmlrpc"));
            XmlRpcClient client=new XmlRpcClient();
            client.setConfig(config);
            Vector<String> param=new Vector<String>();
            param.add("tom");
            String property=(String) client.execute("TestProperty.returnResult", param);
            System.out.println("property:"+property);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }
        
    }

 

<servlet>
        <servlet-name>XmlRpcServer</servlet-name>
        <servlet-class>xiu.rpc.Server</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XmlRpcServer</servlet-name>
        <url-pattern>/sayHello</url-pattern>
    </servlet-mapping>

rpc 3.0 不写servlet,用属性文件来配置javabean

1、创建javabean

2、配置javabean

3、client调用

public class TestProperty {

    public String returnResult(String str){
        return "property,"+str;
    }
}

注:属性文件的路径不能变

TestProperty=xiu.com.TestProperty

 

public static void main(String[] args) {
        XmlRpcClientConfigImpl config=new XmlRpcClientConfigImpl();
        try {
            config.setServerURL(new URL("http://127.0.0.1:8080/rpc3/xmlrpc"));
            XmlRpcClient client=new XmlRpcClient();
            client.setConfig(config);
            Vector<String> param=new Vector<String>();
            param.add("tom");
            String property=(String) client.execute("TestProperty.returnResult", param);
            System.out.println("property:"+property);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }
       
    }

rpc 3.0 通过属性文件来配置服务,还是很方便的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值