1.首先建立一个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代码
<service>
<name>HelloWorldService</name>
<serviceClass>
com.myeclipse.wsExample.IHelloWorldService
</serviceClass>
<implementationClass>
com.myeclipse.wsExample.HelloWorldServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
<service>
<name>HelloWorldService</name>
<serviceClass>
com.myeclipse.wsExample.IHelloWorldService
</serviceClass>
<implementationClass>
责任编辑:小草