在Java中使用XFire,可以按照以下步驟進行:
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-core</artifactId>
<version>1.2.6</version>
</dependency>
public interface MyService {
public String sayHello(String name);
}
public class MyServiceImpl implements MyService {
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
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);
}
}
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]);
}
}