public class WebServiceClient {
private static final String MESSAGE = "Hello World";
private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
public void setDefaultUri(String defaultUri) {
webServiceTemplate.setDefaultUri(defaultUri);
}
public void simpleSendAndReceive() {
StreamSource source = new StreamSource(new StringReader(MESSAGE));
StreamResult result = new StreamResult(System.out);
webServiceTemplate.sendSourceAndReceiveToResult(source, result);
}
}
public class Server extends HttpServlet {
private XmlRpcServletServer server;
public void init(ServletConfig pConfig) throws ServletException {
super.init(pConfig);
try {
server = new XmlRpcServletServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("HelloHandler", HelloHandlerImpl.class);
server.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)server.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
} catch (XmlRpcException e) {
}
}
public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
throws IOException, ServletException {
server.execute(pRequest, pResponse);
}
}
客户端代码:
public class Client {
public static void main(String[] args) {
try {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:8080/jsp/XmlRpcServer"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Vector params = new Vector();
params.addElement("Tom");
String result = (String) client.execute("HelloHandler.sayHello", params);
System.out.println(result);
} catch (Exception e) {
}
}
}
public class Server {
public static void main(String[] args) {
IService service = new ServiceImpl("service");
Context namingContext = new InitialContext();
namingContext.rebind("rmi://localhost/service", service);
}
}
客户端:
public class Client {
public static void main(String[] args) {
String url = "rmi://localhost/";
try {
Context namingContext = new InitialContext();
IService service = (IService) namingContext.lookup(url + "service");
Class stubClass = service.getClass();
System.out.println(service + " 是 " + stubClass.getName() + " 的实例!");
Class[] interfaces = stubClass.getInterfaces();
for (Class c : interfaces) {
System.out.println("存根类实现了 " + c.getName() + " 接口!");
}
System.out.println(service.service("你好!"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
service Hello{
string helloString(1:string para)
i32 helloInt(1:i32 para)
bool helloBoolean(1:bool para)
void helloVoid()
string helloNull()
}
HelloServiceImpl服务实现
public class HelloServiceImpl implements Hello.Iface {
@Override
public boolean helloBoolean(boolean para) throws TException {
return para;
}
@Override
public int helloInt(int para) throws TException {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return para;
}
@Override
public String helloNull() throws TException {
return null;
}
@Override
public String helloString(String para) throws TException {
return para;
}
@Override
public void helloVoid() throws TException {
System.out.println("Hello World");
}
}
服务端:
public class Server {
public static void main(String[] args) {
try {
TServerSocket serverTransport = new TServerSocket(7911);
Factory proFactory = new TBinaryProtocol.Factory();
TProcessor processor = new Hello.Processor(new HelloServiceImpl());
TServer server = new TThreadPoolServer(processor, serverTransport, proFactory);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
客户端:
public class HelloServiceClient {
public static void main(String[] args) {
try {
TTransport transport = new TSocket("localhost", 7911);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
client.helloVoid();
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
}
本文来自网易实践者社区,经作者张震权发布。