古老的 东西,有时候还是不得不使用一下,所以简单介绍下使用。
服务端,pom.xml中增加需要的库
1 2 3 4 5 6
| 1 <dependency>
2 <groupId>org.apache.xmlrpc</groupId>
3 <artifactId>xmlrpc-server</artifactId>
4 <version>3.1.3</version>
5 </dependency>
6 |
如果采用main方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 1package webserver;
2
3import org.apache.xmlrpc.server.PropertyHandlerMapping;
4import org.apache.xmlrpc.server.XmlRpcServer;
5import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
6import org.apache.xmlrpc.webserver.WebServer;
7
8public class Server {
9 private static final int port = 9091;
10
11 public static void main(String[] args) throws Exception {
12 WebServer webServer = new WebServer(port);
13
14 XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
15
16 PropertyHandlerMapping phm = new PropertyHandlerMapping();
17 phm.load(Thread.currentThread().getContextClassLoader(), "MyHandlers.properties");
18 xmlRpcServer.setHandlerMapping(phm);
19 XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
20 serverConfig.setEnabledForExtensions(true);
21 serverConfig.setContentLengthOptional(false);
22
23 webServer.start();
24 }
25
26}
27 |
MyHandlers.properties中 只有一行
1 2
| 1MyCalculator=webserver.MyCalculator
2 |
1 2
| 1 MyCalculator的内容也很简单
2 |
1 2 3 4 5 6 7 8 9
| 1package webserver;
2
3public class MyCalculator {
4 public int myAdd(int i1, int i2) {
5 return i1 + i2 * 10;
6 }
7}
8
9 |
1 2
| 1 启动程序。服务端就完成啦!,当然这个服务端,默认配置的名字是XML_RPC_Server, url 的地址是service
2 |
客户端 pom.xml中增加
1 2 3 4 5 6
| 1 <dependency>
2 <groupId>org.apache.xmlrpc</groupId>
3 <artifactId>xmlrpc-client</artifactId>
4 <version>3.1.3</version>
5 </dependency>
6 |
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 1import java.net.MalformedURLException;
2import java.net.URL;
3
4import org.apache.xmlrpc.XmlRpcException;
5import org.apache.xmlrpc.client.XmlRpcClient;
6import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
7class MyTest {
8 public static void main(String[] args) throws IOException {
9 logger.info("before xml rpc");
10 try {
11
12 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
13 config.setServerURL(new URL("http://127.0.0.1:9091/XML_RPC_Server/service"));
14
15 XmlRpcClient client = new XmlRpcClient();
16 client.setConfig(config);
17
18 Object[] params = new Object[]{new Integer(31), new Integer(9)};
19 Integer result = (Integer) client.execute("MyCalculator.myAdd", params);
20 System.out.println(result);
21
22 } catch (XmlRpcException e) {
23 e.printStackTrace();
24 } catch (MalformedURLException e) {
25 e.printStackTrace();
26 }
27
28 }
29 }
30 |
1 2
| 1 这样就可以成功拿到计算的结果了。可以说是非常的简单。
2 |
当然,服务端也可以从采用tomcat方式启动,对web.xml的配置如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 1<?xml version="1.0" encoding="UTF-8"?>
2<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5 id="WebApp_ID" version="2.5">
6 <display-name>XML_RPC_Server</display-name>
7 <description>XMl_RPC_Server Appliction</description>
8
9 <servlet>
10 <servlet-name>XmlRpcServlet</servlet-name>
11 <servlet-class>org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>
12 <init-param>
13 <param-name>enabledForExtensions</param-name>
14 <param-value>true</param-value>
15 </init-param>
16 </servlet>
17 <servlet-mapping>
18 <servlet-name>XmlRpcServlet</servlet-name>
19 <url-pattern>/service</url-pattern>
20 </servlet-mapping>
21
22 <welcome-file-list>
23 <welcome-file>index.jsp</welcome-file>
24 </welcome-file-list>
25</web-app>
26 |
1 2
| 1 具体请参考官网https://ws.apache.org/xmlrpc/
2 |