In XmlRpc The addHandler method of WebServer class in JAVA is deprecated what is the other alternative for this method?

huangapple go评论74阅读模式
英文:

In XmlRpc The addHandler method of WebServer class in JAVA is deprecated what is the other alternative for this method?

问题

我知道这个问题已经被提问过了。在先前的问题中,有一个链接,可以找到关于org.apache.xmlrpc的详细信息,但我无法确定WebServer类的addHandler方法的替代方法。

我有两个项目

	1    服务器 (Server)
			服务器项目包括
				1.1 区域服务器 (AreaServer)
				1.2 区域处理器 (AreaHandler)
	2	客户端 (Client)
			客户端项目包括
				2.1 客户端 (Client)

AreaHandler类计算给定半径的圆的面积。
AreaServer使用AreaHandler类。

AreaServer

package webtutorial;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.xmlrpc.webserver.WebServer;

/**
 *
 * 作者:Dev Parzival
 */
public class AreaServer {
	public static void main(String $[]){
		try {
			startServer($);
		} catch (IOException ex) {
			Logger.getLogger(AreaServer.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
	public static void startServer(String $[]) throws IOException{
		WebServer server;
		server=new WebServer(Integer.parseInt($[0]));
		server.addHandler("area",new AreaHandler());
		server.start();
	}
}

AreaHandler

package webtutorial;

/**
 *
 * 作者:Dev Parzival
 */
public class AreaHandler {
	
	/**
	 *
	 * @param radius 需要计算面积的圆的半径
	 * @return 圆的面积,其类型为双精度浮点数
	 */
	public double circleArea(double radius){
		return Math.PI*radius*radius;
	}
}

Client

package client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

/**
 *
 * 作者:Dev Parzival
 * 日期:02-Sep-2020
 * 时间:08:46:01
 */
public class Client {

	/**
	 * @param $ 命令行参数,表示要通过服务器计算其面积的圆的半径。
	 */
	public static void main(String[] $) {
		// TODO 应用程序逻辑
		Client client=new Client();
		double radius=Double.parseDouble($[0]);
		try{
			double area=client.areaCircle(radius);
			System.out.println("圆的面积是:"+area);
		}
		catch(Exception ex){
			System.out.println(ex);
		}
	}
	public double areaCircle(double radius) throws IOException,XmlRpcException{
		double area=0;
		XmlRpcClient client=new XmlRpcClient();
		ArrayList<Double> vec=new ArrayList<Double>();
		vec.add(new Double(radius));
		Object result=client.execute("area.circleArea", vec);
		area=Double.parseDouble(result.toString());
		return area;
	}
}

addHandler方法已被弃用,因此我想知道如何做到这一点

server.addHandler("area",new AreaHandler());

将AreaHandler类与区域关联起来,这样当客户端请求服务器时,它会返回面积。

英文:

I know that this question has already been asked .In the previously asked questions there is a link from where we can find the details about org.apache.xmlrpc but I cound not determine the alternative for addHandler method of WebServer class.

I have two projects

	1    Server
			Server project consist of
				1.1 AreaServer
				1.2 AreaHandler
	2	Client
			Client project consist of
		2.1 Client

AreaHandler class copmutes the area of the circle for a given radius.
AreaServer uses AreaHandler class.

AreaServer

package webtutorial;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.xmlrpc.webserver.WebServer;

/**
 *
 * @author Dev Parzival
 */
public class AreaServer {
	public static void main(String $[]){
		try {
			startServer($);
		} catch (IOException ex) {
			Logger.getLogger(AreaServer.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
	public static void startServer(String $[]) throws IOException{
		WebServer server;
		server=new WebServer(Integer.parseInt($[0]));
		server.addHandler(&quot;area&quot;,new AreaHandler());
		server.start();
	}
}

AreaHandler

package webtutorial;

/**
 *
 * @author Dev Parzival
 */
public class AreaHandler {
	
	/**
	 *
	 * @param radius radius of circle whose area has to be computed
	 * @return area of the circle whose type is double
	 */
	public double circleArea(double radius){
		return Math.PI*radius*radius;
	}
}

Client

package client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

/**
 *
 * @author Dev Parzival
 * @date   02-Sep-2020
 * @time   08:46:01
 */
public class Client {

	/**
	 * @param $ the command line arguments , represent the radius of the circle whose area is to be computer via the server.
	 */
	public static void main(String[] $) {
		// TODO code application logic here
		Client client=new Client();
		double radius=Double.parseDouble($[0]);
		try{
			double area=client.areaCircle(radius);
			System.out.println(&quot;Area of the circle is : &quot;+area);
		}
		catch(Exception ex){
			System.out.println(ex);
		}
	}
	public double areaCircle(double radius) throws IOException,XmlRpcException{
		double area=0;
		XmlRpcClient client=new XmlRpcClient();
		ArrayList&lt;Double&gt; vec=new ArrayList&lt;Double&gt;();
		vec.add(new Double(radius));
		Object result=client.execute(&quot;area.circleArea&quot;, vec);
		area=Double.parseDouble(result.toString());
		return area;
	}
}

The addHandler method is deprecated so I want to know how can do this

server.addHandler(&quot;area&quot;,new AreaHandler());

Link the AreaHandler class with area So when client request the server it returns the area.

答案1

得分: 1

你仍然可以使用已弃用的方法。您正在使用哪个版本的xmlrpc?

文档表明您可以使用PropertyHandlerMapping从属性文件中添加映射。

PropertyHandlerMapping phm = new PropertyHandlerMapping();
/* 从属性文件中加载处理程序定义。
 * 属性文件可能类似于:
 *   Calculator=org.apache.xmlrpc.demo.Calculator
 *   org.apache.xmlrpc.demo.proxy.Adder=org.apache.xmlrpc.demo.proxy.AdderImpl
 */
phm.load(Thread.currentThread().getContextClassLoader(),
         "MyHandlers.properties");
xmlRpcServer.setHandlerMapping(phm);

或者您也可以直接添加它们:

/* 您还可以直接提供处理程序类,
 * 就像这样:
 * phm.addHandler("Calculator",
 *     org.apache.xmlrpc.demo.Calculator.class);
 * phm.addHandler(org.apache.xmlrpc.demo.proxy.Adder.class.getName(),
 *     org.apache.xmlrpc.demo.proxy.AdderImpl.class);
 */

参见:https://ws.apache.org/xmlrpc/server.html

您仍然可以像这样添加处理程序:

WebServer server = new WebServer(Integer.parseInt($[0]));
PropertyHandlerMapping handlerMapping = new PropertyHandlerMapping();
handlerMapping.addHandler("area", AreaHandler.class);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
xmlRpcServer.setHandlerMapping(handlerMapping);

XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);

server.start();
英文:

You can still use deprecated methods. What version of xmlrpc are you using?

The docs indicate that you can use PropertyHandlerMapping to add mappings from a property file.

      PropertyHandlerMapping phm = new PropertyHandlerMapping();
      /* Load handler definitions from a property file.
       * The property file might look like:
       *   Calculator=org.apache.xmlrpc.demo.Calculator
       *   org.apache.xmlrpc.demo.proxy.Adder=org.apache.xmlrpc.demo.proxy.AdderImpl
       */
     phm.load(Thread.currentThread().getContextClassLoader(),
               &quot;MyHandlers.properties&quot;);
     xmlRpcServer.setHandlerMapping(phm);

Or you can add them directly:

      /* You may also provide the handler classes directly,
       * like this:
       * phm.addHandler(&quot;Calculator&quot;,
       *     org.apache.xmlrpc.demo.Calculator.class);
       * phm.addHandler(org.apache.xmlrpc.demo.proxy.Adder.class.getName(),
       *     org.apache.xmlrpc.demo.proxy.AdderImpl.class);
       */

See: https://ws.apache.org/xmlrpc/server.html

You can still add a handler, like this:

WebServer server = new WebServer(Integer.parseInt($[0]));
PropertyHandlerMapping handlerMapping = new PropertyHandlerMapping();
handlerMapping.addHandler(&quot;area&quot;, AreaHandler.class);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
xmlRpcServer.setHandlerMapping(handlerMapping);

XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);

server.start();

huangapple
  • 本文由 发表于 2020年9月2日 11:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定