扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java涓瓁fire濡備綍浣跨敤

扬州沐宇科技
2023-08-22 17:49:26
Java, xfire

鍦↗ava涓娇鐢╔Fire锛屽彲浠ユ寜鐓т互涓嬫楠よ繘琛岋細

  1. 寮曞叆XFire鐨勪緷璧栧簱銆傚彲浠ラ€氳繃鍦∕aven椤圭洰鐨刾om.xml鏂囦欢涓坊鍔犱互涓嬩緷璧栨潵寮曞叆XFire锛?/li>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-core</artifactId>
<version>1.2.6</version>
</dependency>
  1. 鍒涘缓鏈嶅姟鎺ュ彛銆傚垱寤轰竴涓狫ava鎺ュ彛锛屽畾涔夐渶瑕佹彁渚涚殑鏈嶅姟鏂规硶銆?/li>
public interface MyService {
public String sayHello(String name);
}
  1. 瀹炵幇鏈嶅姟鎺ュ彛銆傚垱寤轰竴涓狫ava绫伙紝瀹炵幇鏈嶅姟鎺ュ彛涓畾涔夌殑鏂规硶銆?/li>
public class MyServiceImpl implements MyService {
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
  1. 閰嶇疆鏈嶅姟绔€傚垱寤轰竴涓湇鍔$绋嬪簭锛屽皢鏈嶅姟鎺ュ彛鐨勫疄鐜扮被鍙戝竷涓篧eb鏈嶅姟銆?/li>
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.XFireServlet;
import javax.servlet.ServletException;
public class MyServiceServer extends XFireServlet {
public void init() throws ServletException {
super.init();
Service serviceModel = new ObjectServiceFactory().create(MyService.class);
serviceModel.setInvoker(new MyServiceImpl());
XFire xfire = XFireFactory.newInstance().getXFire();
xfire.getServiceRegistry().register(serviceModel);
}
}
  1. 閰嶇疆瀹㈡埛绔€傚垱寤轰竴涓鎴风绋嬪簭锛岄€氳繃XFire璋冪敤杩滅▼鏈嶅姟銆?/li>
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class MyServiceClient {
public static void main(String[] args) throws Exception {
Service serviceModel = new ObjectServiceFactory().create(MyService.class);
Client client = new Client(serviceModel, "http://localhost:8080/MyServiceServer");
Object[] result = client.invoke("sayHello", new Object[]{"John"});
System.out.println(result[0]);
}
}
  1. 閮ㄧ讲鍜岃繍琛屻€傚皢鏈嶅姟绔▼搴忛儴缃插湪涓€涓猄ervlet瀹瑰櫒涓紙濡俆omcat锛夛紝杩愯鏈嶅姟绔▼搴忋€傜劧鍚庤繍琛屽鎴风绋嬪簭锛屽嵆鍙€氳繃XFire璋冪敤杩滅▼鏈嶅姟銆?/li>

扫码添加客服微信