Java客户端调用Webservice接口求代码
发布网友
发布时间:2022-04-30 20:15
我来回答
共2个回答
热心网友
时间:2022-04-24 05:51
客户端获得自定义对象包路径必须和服务器端相同,当然在客户端也可以不用建该对象,可以将客户端的自定义对象打成jar包,然后在客户端引用。
猜想用反射也可以实现改对象,但目前没有写相关例子。
<p>import java.io.Serializable;</p><p>public class Hello implements Serializable {
/**
*客户端必须有与服务器端相同的自定义对象
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}</p>
[java] view plain copy
package client;
import po.Hello;
public interface IClientHelloManager {
/*
[java] view plain copy
*该方法名必须和服务接口一致
[java] view plain copy
*/
[java] view plain copy
public Hello example();
[java] view plain copy
<pre class="java" name="code">package client;
import java.net.MalformedURLException; //import java.net.URL;
//import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import po.Hello;
public class ServicesClient {
public static void main(String[] arg) throws MalformedURLException,
Exception {
String xml = "http://localhost:8080/web/services/HeloWebService";
ObjectServiceFactory objectServiceFactory = new ObjectServiceFactory();
Service serviceModel = objectServiceFactory
.create(IClientHelloManager.class);
XFireProxyFactory xFireProxyFactory = new XFireProxyFactory();
IClientHelloManager service = (IClientHelloManager) xFireProxyFactory
.create(serviceModel, xml);
Hello lHello = service.example();
System.out.println(lHello.getId());
System.out.println(lHello.getName());
// Client client = new Client(new URL(
// "http://localhost:8080/web/services/HeloWebService?wsdl"));
// Object[] rsult = client.invoke("example", new Object[] { "hello" });
// Hello hello = (Hello) rsult[0];
// System.out.println();
}
}</pre>
<pre></pre>
<pre></pre>
<pre></pre>
热心网友
时间:2022-04-24 07:09
http://blog.csdn.net/kongling16688/article/details/7938841