您的位置:首页 > 服装鞋帽 > 男装 > java调用webservice的各种方法总结1

java调用webservice的各种方法总结1

luyued 发布于 2011-05-29 11:18   浏览 N 次  
一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务首先建立一个Web services EndPoint

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代码

HelloWorldService

com.myeclipse.wsExample.IHelloWorldService

com.myeclipse.wsExample.HelloWorldServiceImpl

literal

application

HelloWorldService

com.myeclipse.wsExample.IHelloWorldService

com.myeclipse.wsExample.HelloWorldServiceImpl

literal

application

把整个项目部署到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[]{});

}

广告赞助商