用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工具进行请求的监控
编码:
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工具进行请求的监控
相关文章推荐
- java.net.BindException: Address already in use: JVM_Bind <null>:8080错误
- Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
- 【小熊刷题】3 Sum <Leetcode 15,Java>
- Arduino + Eclipse 开发环境搭建
- 大龄屌丝自学笔记--Java零基础到菜鸟--017
- 深入理解Java:注解(Annotation)--注解处理器
- 深入理解Java:注解(Annotation)自定义注解入门
- java子父类的加载顺序
- Java中equals和==的区别
- IO模型-java版
- JDK配置环境变量方法
- java设计模式第6弹--单例模式
- 编译hadoop的eclipse插件hadoop-eclipse-plugin-1.2.1.jar
- Java接口和抽象类的区别
- Spring使用Cache、整合Ehcache
- 队列 java
- STRUTS2为每个线程提供一个ACTION实例
- MyBatis3整合Spring3、SpringMVC3
- Java基础
- Java垃圾回收_过程观察