您的位置:首页 > 编程语言 > Java开发

用JDK开发web service

2015-09-11 23:31 651 查看
服务器端

编码:

a. 创建一个基于jdk6以上版本的java工程

b. 定义SEI web service Endpoint interface(web service终端接口)

@WebService

public interface HelloWS {

@WebMethod

public String sayHello(String name);

}

c. 定义SEI的实现类:

@WebService

public class HelloWSImpl implements HelloWS {

@Override

public String sayHello(String name) {

System.out.println("sayHello "+name);

return "hello "+name;

}

}

发布:

public class Server {

public static void main(String[] args) {

//客户端发送web service请求的url

String address="http://127.0.0.1/tg_ws/helloWS";

//处理请求的SEI对象

HelloWS helloWS = new HelloWSImpl();

//发布web service

Endpoint.publish(address, helloWS);

System.out.println("发布web service成功!");

}

}

(注:我的要建一个ServerDelegate 在Server基础上点击 打开new=》other=》输入 web service 点击=》选择 create web service from java class=》打开Brouwse 输入你的测试类 最后勾上 Generate WSDL in project 会生成一个 * Delegate.java)

②. 客户端

1. eclipse Web Service浏览器

a. 查看Web Service所对应的WSDL文档:...?wsdl

b. 使用eclipse访问

请求体:SOAP Request Envelope

<soapenv:Envelope>

<soapenv:Body>

<q0:sayHello>

<arg0>tt</arg0>

</q0:sayHello>

</soapenv:Body>

</soapenv:Envelope>

响应体:SOAP Response Envelope

<S:Envelope>

<S:Body>

<ns2:sayHelloResponse xmlns:ns2="http://ws.java.atguigu.net/">

<return>hello tt</return>

</ns2:sayHelloResponse>

</S:Body>

</S:Envelope>

2. 编码实现

a. 创建客户端java应用

b. 在应用的src下执行cxf的命令生成客户端代码:

wsimport -keep http://127.0.0.1/tg_ws/helloWS?wsdl
c. 编写客户端调用的测试代码,执行:

public class Client {

public static void main(String[] args) {

//创建SEI的工厂对象

HelloWSImplService factory = new HelloWSImplService();

//得到一个SEI实现类对象

HelloWSImpl helloWS = factory.getHelloWSImplPort();

//调用SEI的方法,此时才去发送web Service请求,并得到返回结果

String result = helloWS.sayHello("Tom");

System.out.println(result);

}

}



③.请求过程记录:使用eclipse的tcp/ip工具进行请求的监控
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: