Java客户端调用Webservice接口求代码
2个回答
2016-11-15
展开全部
客户端获得自定义对象包路径必须和服务器端相同,当然在客户端也可以不用建该对象,可以将客户端的自定义对象打成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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询