在XmlRpc中,JAVA中WebServer类的addHandler方法已被弃用。该方法的其他替代方法是什么?
我知道这个问题已经被问过了。在前面的问题中有一个链接,我们可以从中找到关于org.apache.xmlrpc的详细信息,但我无法确定WebServer类的addHandler方法的替代方法 我有两个项目在XmlRpc中,JAVA中WebServer类的addHandler方法已被弃用。该方法的其他替代方法是什么?,java,server,client,xml-rpc,Java,Server,Client,Xml Rpc,我知道这个问题已经被问过了。在前面的问题中有一个链接,我们可以从中找到关于org.apache.xmlrpc的详细信息,但我无法确定WebServer类的addHandler方法的替代方法 我有两个项目 1 Server Server project consist of 1.1 AreaServer 1.2 AreaHandler 2 Client Clie
1 Server
Server project consist of
1.1 AreaServer
1.2 AreaHandler
2 Client
Client project consist of
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;
/**
*
* @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("area",new AreaHandler());
server.start();
}
}
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;
}
}
AreaHandler
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("area",new AreaHandler());
server.start();
}
}
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;
}
}
客户端
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("Area of the circle is : "+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;
}
}
将AreaHandler类与area链接,以便在客户端请求服务器时返回该区域。您仍然可以使用不推荐的方法。您使用的是什么版本的xmlrpc 这些文档表明您可以使用
PropertyHandlerMapping
从属性文件添加映射
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(),
"MyHandlers.properties");
xmlRpcServer.setHandlerMapping(phm);
或者您可以直接添加它们:
/* You may also provide the handler classes directly,
* like this:
* 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);
*/
见:
您仍然可以添加处理程序,如下所示:
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();
jar版本是3.1.3如果您可以共享旧版本xmlrpc jar文件的链接,以便它支持addHandler MethodUpdate answer,您应该仍然能够添加处理程序,但可以使用属性处理程序映射类来完成。我在线程“main”中获取此错误异常java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory位于org.apache.xmlrpc.server.XmlRpcStreamServer。(未知源)位于org.apache.xmlrpc.webserver.webserver.newXmlRpcStreamServer(未知源)位于org.apache.xmlrpc.webserver.webserver。(未知源);在这一行:server=newwebserver(Integer.parseInt($[0])//$[0]=4404