java调用webservice的各种方法总结1
luyued 发布于 2011-05-29 11:18 浏览 N 次Java代码
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "“n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "“n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
2.使用 apt 编译 Hello.java(例:apt -d [存放编译后的文件目录] Hello.java ) ,会生成 jaws目录
3.使用java Hello.Hello运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出现下列显示
4.使用wsimport 生成客户端
使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl
5.客户端程序
Java代码
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的
利用xfire开发WebService,可以有三种方法
1一种是从javabean 中生成;
2 一种是从wsdl文件中生成;
3 还有一种是自己建立webservice
步骤如下
用myeclipse建立webservice工程,目录结构如下
首先建立webservice接口,
代码如下
Java代码
package com.myeclipse.wsExample;
//Generated by MyEclipse
public interface IHelloWorldService {
public String example(String message);
}
package com.myeclipse.wsExample;
//Generated by MyEclipse
public interface IHelloWorldService {
public String example(String message);
}
Java代码
package com.myeclipse.wsExample;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService {
public String example(String message) {
return message;
}
}
package com.myeclipse.wsExample;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService {
public String example(String message) {
return message;
}
}
修改service.xml 文件,加入以下代码
Xml代码
com.myeclipse.wsExample.IHelloWorldService
com.myeclipse.wsExample.HelloWorldServiceImpl
com.myeclipse.wsExample.IHelloWorldService
com.myeclipse.wsExample.HelloWorldServiceImpl
把整个项目部署到tomcat服务器中 ,打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl
客户端实现如下
Java代码
package com.myeclipse.wsExample.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFireFactory;
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 com.myeclipse.wsExample.IHelloWorldService;
public class HelloWorldClient {
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url="http://localhost:8989/HelloWorld/services/HelloWorldService";
try
{
IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);
String st=hs.example("zhangjin");
System.out.print(st);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package com.myeclipse.wsExample.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFireFactory;
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 com.myeclipse.wsExample.IHelloWorldService;
public class HelloWorldClient {
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url="http://localhost:8989/HelloWorld/services/HelloWorldService";
try
{
IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);
String st=hs.example("zhangjin");
System.out.print(st);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这里再说点题外话,有时候我们知道一个wsdl地址,比如想用java客户端引用.net 做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下
Java代码
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//远程调用.net开发的webservice
Client c=new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});
//调用.net本机开发的webservice
Client c1=new Client(new URL("http://localhost/zj/Service.asmx?wsdl"));
Object[] o1=c1.invoke("HelloWorld",new String[]{});
}
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//远程调用.net开发的webservice
Client c=new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});
//调用.net本机开发的webservice
Client c1=new Client(new URL("http://localhost/zj/Service.asmx?wsdl"));
Object[] o1=c1.invoke("HelloWorld",new String[]{});
}
- 07-01· 北凉国的那些事(组诗)
- 07-01· 《乱世佛音》 第七章 巨变
- 07-01· 南匈奴始末(3)
- 06-30· 流浪在波希米尼亚
- 06-30· 希尼亚从大洋西岸放飞新
- 06-28· 瑪利亞之城 - 家庭日 "光
- 06-28· 至青年营弟兄姐妹的一封
- 06-26· 《三国群英大富翁》追忆
- 06-24· 东莞血汗工厂实录(281:沙田
- 06-22· 第一次看戏
- 06-22· 经典复刻,独一无二:试
- 06-22· 蓝旗营教学中心9月份盛大
- 06-22· 品牌折扣女装嫣然品牌折
- 06-21· IQVopdnkvdz 1100
- 06-21· kriyoylto8fyds'p;tyijyfuifiogoi
- 06-21· 巴黎春天缤纷圣诞 喜迎新
- 06-21· 晒JS宝宝贝贝些 咯
- 06-21· 司马氏的谥法和葬仪
- 06-21· [转载]司马氏的谥法和葬仪
- 06-21· 闲来蓟县看秋山