Java 中的 Web Service 分为基于 SOAP 的和基于 REST 的两种,下面简单说一个基于 SOAP 的例子。要使用 JDK6u4 之后的版本才能编译通过。
先编写一个 Web Service 的接口:
package com;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface TimeServer {
@WebMethod String getTimeAsstring();
@WebMethod long getTimeAsElapsed();
}
package com;
import java.util.Date;
import javax.jws.WebService;
@WebService(endpointInterface = "com.TimeServer")
public class TimeServerImpl implements TimeServer {
public String getTimeAsstring() { return new Date().toString(); }
public long getTimeAsElapsed() { return new Date().getTime(); }
}
package com;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher {
public static void main(String[ ] args) {
TimeServerImpl tt= new TimeServerImpl();
Endpoint.publish("http://127.0.0.1:9876/ts",tt );
}
}
如果正常启动,可以用浏览器访问 http://127.0.0.1:9876/ts?wsdl 看到这个 Web Service 的 wsdl 文档。
转载http://zhidao.baidu.com/link?url=8QY81dR99QHJn1eNtOkMswopFR122Sl9IJg9s8c_r5h7M2-xtCrHed3ldJdV-NvSZu9ESSAV0sPX_hicxgmbDJOE6WinI9brpHRZPwFefhe
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。