webservice - 使用JAX-WS注解的方式快速搭建服务端和客户端
2024-10-07 21:56:30
1.Define the interface
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC)
public interface ISay {
@WebMethod(operationName="HansayHello")
@WebResult(name="myReturn")
public String sayHello(@WebParam(name="name") String name); @WebMethod(operationName="HansayGoodBye")
@WebResult(name="myReturn")
public String sayGoodbye(@WebParam(name="name")String name); @WebMethod(exclude=true)//当前方法不被发布出去
public String sayHello2(@WebParam(name="name")String name);
}
2.Implement the interface
import javax.jws.WebService; /**
* WebService
* 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
*/
@WebService(endpointInterface = "com.mobile263.ISay")
public class SayImpl implements ISay { public String sayHello(String name){
System.out.println("sayHello got the request:"+name);
return "hello: " + name;
} public String sayGoodbye(String name){
System.out.println("sayGoodBye got the request:"+name);
return "goodbye: " + name;
} public String sayHello2(String name){
return "hello " + name;
} }
3. Publish the ws in server
import javax.xml.ws.Endpoint; /**
* WebService
* 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
*/ public class Wsserver { public static void main(String[] args) {
/**
* 参数1:服务的发布地址
* 参数2:服务的实现者
* Endpoint 会重新启动一个线程
*/
Endpoint.publish("http://localhost:8090/WStest/com.mobile.Wsserver", new SayImpl());
System.out.println("Server ready...");
} }
4. Input the link on brouswer and enter,the wsdl will be shown. --> http://localhost:8090/WStest/com.mobile.Wsserver?wsdl
5. Use wsimport to export the client files
wsimport -d d: -keep -verbose http://localhost:8090/WStest/com.mobile.Wsserver?wsdl
最新文章
- POJMatrix(二维树状数组)
- jsp EL 表达式
- php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项
- C#自定义导出数据到Excel中的类封装
- mongo中查询Array类型的字段中元素个数
- 如何通过图片在 HTTPS 网站中获取 HTTP 接口数据
- apache 实用配置
- ActionBar 中setDisplayHomeAsUpEnabled和setHomeButtonEnabled和setDisplayShowHomeEnabled方法的理解
- FZU Problem 2150 Fire Game(bfs)
- JS原型--原型链
- 一、Swagger配置
- jdk下载及环境变量配置
- Migrating your code from 9.3 to 10.x
- Java编程的逻辑 (90) - 正则表达式 (下 - 剖析常见表达式)
- JBoss 7 里一个EJB依赖其他jar的几种方式
- 88个 Linux 系统管理员必备的监控工具
- hdu-4283 You Are the One 区间dp,
- 设置ubuntu 终端显示路径长度
- java程序调优系列(一)intern()代替equal()
- Python执行Linux系统命令方法