成都市科技馆预约系统:利用JAX-WS开发Web服务 - 架构与设计 -
来源:百度文库 编辑:中财网 时间:2024/10/06 04:03:10
本文提供了一个使用Java如何开发基于SOAP的Web Services,其客户端可以是Perl、Ruby、Python或Java等。
Java SE 6封装了JAX-WS(Java API for XML-WebServices),而JAX-WS同时支持基于SOAP的Web服务和REST风格的Web服务。JAX-WS通常可简写为JWS,当前,JWS的版本为2.x。
基于SOAP的Web服务可用单个Java类的实现,但是最好是用“接口+实现”的方式来实现最佳。
Web服务的接口称为SEI,即Service Endpoint Interface;
而Web服务的实现称为SIB,即Service Implementation Bean。
SIB可以是一个POJO,也可以是无状态的会话EJB。本文的SIB是普通Java类,通过JDK 6的类库即可实现Web服务的发布。
代码1:服务接口类SEI
viewplaincopyto clipboardprint?- package myweb.service;
- import javax.jws.WebService;
- import javax.jws.WebMethod;
- import javax.jws.soap.SOAPBinding;
- import javax.jws.soap.SOAPBinding.Style;
- @WebService
- @SOAPBinding(s=Style.RPC)
- public interface TimeServer {
- @WebMethod
- String getTimeAsString();
- @WebMethod
- long getTimeAsElapsed();
- }
代码2:服务实现类SIB
viewplaincopyto clipboardprint?- package myweb.service;
- import java.text.DateFormat;
- import java.util.Date;
- import javax.jws.WebService;
- @WebService(endpointInterface = "myweb.service.TimeServer")
- public class TimeServerImpl implements TimeServer {
- /**
- * 返回从1970年1月1日0点0时0分起的毫秒数
- */
- public long getTimeAsElapsed() {
- return new Date().getTime();
- }
- /**
- * 返回如“2009-12-21”格式的日期
- */
- public String getTimeAsString() {
- Date date = new Date();
- DateFormat df = DateFormat.getDateInstance();
- return df.format(date);
- }
- }
代码3:服务发布类Publisher
viewplaincopyto clipboardprint?- package myweb.service;
- import javax.xml.ws.Endpoint;
- public class TimeServerPublisher {
- public static void main(String[] args){
- // 第一个参数是发布的URL
- // 第二个参数是SIB实现
- Endpoint.publish("http://127.0.0.1:10100/myweb", new TimeServerImpl());
- }
- }
编译以上代码:
javac myweb/service/*.java
运行服务:
java myweb/service/TimeServerPublisher
在浏览器地址栏输入:
http://localhost:10100/myweb?wsdl
显示如下图所示:
也可编写客户端代码测试服务。
Java客户端:
viewplaincopyto clipboardprint?- package myweb.client;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import java.net.URL;
- import myweb.service.*;
- public class TimeClient {
- public static void main(String[] args) throws Exception{
- URL url = new URL("http://localhost:10100/myweb?wsdl");
- // 第一个参数是服务的URI
- // 第二个参数是在WSDL发布的服务名
- QName qname = new QName("http://service.myweb/","TimeServerImplService");
- // 创建服务
- Service service = Service.create(url, qname);
- // 提取端点接口,服务“端口”。
- TimeServer eif = service.getPort(TimeServer.class);
- System.out.println(eif.getTimeAsString());
- System.out.println(eif.getTimeAsElapsed());
- }
- }
运行客户端,显示结果如下:
2009-12-21
1261402511859
也可用Ruby编写客户端,如下:
viewplaincopyto clipboardprint?- #!/usr/bin/ruby
- # one Ruby package for SOAP-based services
- require 'soap/wsdlDriver'
- wsdl_url = 'http://127.0.0.1:10100/myweb?wsdl'
- service = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver
- # Save request/response messages in files named '...soapmsgs...'
- service.wiredump_file_base = 'soapmsgs'
- # Invoke service operations.
- result1 = service.getTimeAsString
- result2 = service.getTimeAsElapsed
- # Output results.
- puts "Current time is: #{result1}"
- puts "Elapsed milliseconds from the epoch: #{result2}"
运行结果相同!
如何用jax做web service?
web 三层架构
什么是struct开发架构
Web Service技术的概念?在Delphi环境下如何开发一个Web服务?
开发基于WIINDOWS的WEB服务程序,该如何搭建开发环境
服务组织架构的格式
NET软件工程师 WEB开发
Web Service 开发教程
web开发语言
web开发工具
编程和web开发
web程序开发
什么是JAX?
如何用IIS架构WEB网站
adsl家庭用户架构web 服务器
web服务与www服务
开发和利用太阳能
什么是web服务
web服务有那些
关于设WEB服务?
web服务站点
web客户端服务是什么意思
什么是WEB服务,FTP 服务,SMTP服务!
Web开发问题(急)