WebService入门篇
首先新建一个webservice服务器端的程序。注意的是要使用jdk6及其以上版本。 ? 将一个项目如下: 内部代码如下: package cn.itcast.server; ? import javax.jws.WebService; import javax.xml.ws.Endpoint; ? @WebService public class Hello { ????? /** ????? ?* Endpoint Web服务端点 ????? ?* 使用在此类定义的静态方法创建端点。一个端点总是绑定到一个Binding ????? ?* 和一个实现者,这两项都是在创建端点时设置的。 ????? ?* ????? ?* 端点要么处于已发布状态,要么处于未发布状态。可以使用publish方法 ????? ?* 开始发布端点,此时端点开始接受传入请求。相反,可以使用stop方法停 ????? ?* 止接受传入请求并取消端点。一旦停止,就不能再次发布端点。 ????? ?* ????? ?* 可以在端点上设置Executor以便更好地控制用来指派传入请求的线程。例如 ????? ?* ,通过创建ThreadPoolExecutor并向端点注册可以启动带有特定参数的线程池。 ????? ?* ????? ?* 处理程序链可以使用所含的Binding来设置。 ????? ?* ????? ?* 端点可以使一个数据文档(如WSDL和XMLSchema文档)列表与之绑定。发布时, ????? ?* JAX-WS实现将根据实现者上存在的注释,尽可能多地重用这些元数据, ????? ?* 而不是生成新的数据 ????? ?* ????? ?* address:一个URI,指定要使用的地址和传输协议 ????? ?* implementor:端点实现者,表示是哪个类实现的服务。 ????? ?*/ ????? publicString sayHi(String name){ ?????????????? System.out.println("姓名? name = " + name); ?????????????? return"欢迎访问我:hi!" + name; ????? } ????? ????? publicstatic void main(String[] args) { //这里的地址可以是随便写的,表示的意思是访问这个webservice的地址是多少。//当然也可以把这个地址改成一个确定的ip地址。 //注意后面的链接地址是在程序中定义的address,要注意的是端口号不能和本机程序中的端口号重名。 ?????????????? Endpoint.publish("http://localhost:8888/one",new Hello()); ????? } } ? @WebService:说明把这个类发布出去的时候是以网络的方式发不出去的。 这个方法一定是public 的,并且还要是非静态的。 ? 接着可以在浏览器地址上输入:http://localhost:8888/one,这时候就可以看到可以被访问到了, 当访问:http://localhost:8888/one?wsdl时可以看到webservice使用说明书。 常见错误: 错误1: 2013-6-12 11:01:53com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass 信息: Dynamically creating request wrapper Classcn.itcast.server.jaxws.SayHi 2013-6-12 11:01:53com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass 信息: Dynamically creating response wrapper bean Classcn.itcast.server.jaxws.SayHiResponse Exception in thread"main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error:java.net.BindException: Address already in use: bind ????? atcom.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(UnknownSource) ????? at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.publish(UnknownSource) ????? atcom.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(UnknownSource) ????? atcom.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(UnknownSource) ????? at javax.xml.ws.Endpoint.publish(UnknownSource) ????? at cn.itcast.server.Hello.main(Hello.java:35) Caused by: java.net.BindException: Address already in use: bind ????? at sun.nio.ch.Net.bind(Native Method) ????? atsun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ????? at sun.nio.ch.ServerSocketAdaptor.bind(UnknownSource) ????? atsun.net.httpserver.ServerImpl.<init>(Unknown Source) ????? atsun.net.httpserver.HttpServerImpl.<init>(Unknown Source) ????? atsun.net.httpserver.DefaultHttpServerProvider.createHttpServer(Unknown Source) ????? at com.sun.net.httpserver.HttpServer.create(UnknownSource) ????? ... 6 more 出现这种错误的原因是因为已经有一个在运行了,也就是说重新运行程序了。 ?????? 错误2 2013-6-12 11:01:53com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass 信息: Dynamically creating request wrapper Classcn.itcast.server.jaxws.SayHi 2013-6-12 11:01:53 com.sun.xml.internal.ws.model.RuntimeModelergetResponseWrapperClass 信息: Dynamically creating response wrapper beanClass cn.itcast.server.jaxws.SayHiResponse Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error:java.net.BindException: Address already in use: bind ????? atcom.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(UnknownSource) ????? atcom.sun.xml.internal.ws.transport.http.server.HttpEndpoint.publish(UnknownSource) ????? atcom.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(UnknownSource) ????? atcom.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(UnknownSource) ????? atjavax.xml.ws.Endpoint.publish(Unknown Source) ????? atcn.itcast.server.Hello.main(Hello.java:35) Caused by: java.net.BindException: Address already in use: bind ????? atsun.nio.ch.Net.bind(NativeMethod) ????? atsun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ????? atsun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ????? atsun.net.httpserver.ServerImpl.<init>(Unknown Source) ????? atsun.net.httpserver.HttpServerImpl.<init>(Unknown Source) ????? atsun.net.httpserver.DefaultHttpServerProvider.createHttpServer(Unknown Source) ????? atcom.sun.net.httpserver.HttpServer.create(Unknown Source) ????? ...6 more 关于jdk中的bin中,native2ascii.exe,这是一个转换ascii的工具。 (1)查看java的jdk是否是jdk6,可以在cmd命令中使用java –version. (2)通过命令的方式进入jdk的bin目录,命令如下:C:UsersAdministrator>setpath=C:Program Files (x86)javajdk1.6.0_25bin; (3)进入之后点击:keytool,可以拥有加密。 ?(4)如果使用webservice需要使用的是:wsimport.exe ?-d:在哪里生成输出文件 -p:指定目标包,通过-p可以生成一个自定义包结构 -s:指定在哪里生成源文件。 ? 以下是将wsdl文件放置到指定目录下的方式,下面的命令表示的意思是将文件放在D:ws目录中。 C:UsersAdministrator>D: D:&;cdws //下面使用jdk中的wsimport.exe工具将文件放在当前文件目录(当前目录通过”.”来表示)。 D:ws>wsimport-d . http://localhost:8888/one?wsdl ? 通过这种方式就可以生成class文件。D盘中的ws目录里的内容如下: ? 获得java文件的方式: 在命令行中输入:D:ws>wsimport-s . http://localhost:8888/one?wsdl 接着ws目录就可以看到如下: ? ??? 通过这种方式就可以得到客户端的代码。 ??? 创建客户端代码,然后将代码拷贝到src中,然后并移动到指定包下。 ??? 当想看webservice使用说明书的时候,最先看里面的<service name=“HelloService”>,表示的意思是使用HelloService来调用,如果看到上面使用的代码,是可以看到有一个HelloService.java类的。 <servicename="HelloService"> <portname="HelloPort"binding="tns:HelloPortBinding"> <soap:address location="http://localhost:8888/one"/> </port> </service> 如上表示的soap:address location=“http://localhost:8888/one”,表示的是注册地址。 ? 提供webservice接口的网站:http://www.36wu.com/Service.aspx 下面的一个网站提供了很多接口服务 http://www.webxml.com.cn/zh_cn/information.aspx ? 下面是关于webservice方面的其它一些知识点 WebService day01 1.概述 JDK1.6_1.3以上的版本才能使用 通过IE访问时,http请求行中:Connection:keep-Alive webService的规范就是http规范+soap规范 WebService的框架: ??? 云计算、云查杀、SOA(面向服务的框架) webService只采用http post方式传输数据,不使用get方式 ??? (http的contentType:applicaiton-x-www-urlencoded) ??? 根据contentType判断soap版本: ??? Text/xml:charset=UTF-8?? ==>soap1.1 ??? application/soap+xml ==>soap1.2 ? ??? 注意:wsimport.exe只能编译soap1.1的版本,不能编译soap1.2 2.helloworld ??? 1.一个类上添加注解@WebService ??? @WebService的成员变量可以查看API。 ??? 2.类中使用Endpoint,并调用Endpoint的publish()方法将此类发布 !!! 注意:要发布的这个类,至少包含一个public 且非static类型的方法. ??? publish()中的参数可以指定发布的路径==>如:http://192.168.1.100:8888/one ??? 发布后会生成一个使用说明书(.xml文件) ??? 3.wsimport生成客户端代码 ??? 4.通过客户端代码调用sayHi() ??? ... ??? Hello? hello = new HelloService().getHelloPort();==> ??? hello.sayHi("张无忌"); ? 3.webService的使用说明书: 对象被发布后会生成一个使用说明书(.xml文件) ??? http://192.168.1.249:8888/one?wsdl ??? wsdl==>web servicedescript language ??? 1.<servicename="HelloService"> ?????? <portname="HelloPort" binding="tns:HelloPortBean"> ?????????? <soap:addresslocation="http://192.168.1.100:8888/one" > ?????? </port> ??? </service> ??? 2.<bindingname="" type="tns:Hello"/> ??? 3.<portTypename=""> ?????? ??? </portType> ? weblogic和oracle装完之后默认会修改我们的环境变量(如jdk等). 所以JDK等的版本也会被更改,我们可以通过环境变量手工改过来. 4.jdk/bin/keytool.exe工具?? ??? 5.jdk/bin/keytool.exe/wsimport.exe工具 ??? 使用方法: wsimport [options]<WSDL_URI> ??? option: ?????? -p <pkg>????? ?specifies the target package ?????? -d<directory>??????????? specifywhere to place generated output files ?????? -s<directory>??????????? specifywhere to place generated source files ??? wsimport -d .http://...==>生成路径中对应的类的.class文件 ??? wsimport -s .http://...==>生成路径中对应的类的.java文件 ? !!! 注意:wsimport.exe只能编译soap1.1的版本,不能编译soap1.2 ? soap协议==>基于xml编码的文本协议 6. webService只采用http post方式传输数据,不使用get方式 ??? (http的contentType:applicaiton-x-www-urlencoded) ??? 根据contentType判断soap版本: ??? Text/xml:charset=UTF-8?? ==>soap1.1 ??? application/soap+xml ==>soap1.2 webService传递参数 ??? (http中传递参数 ...get/?name=zhangsan&age=88) ??? <? xmlversion=1.0?> ??? <name>zhangsan</name> ??? <age>88</age> ? 7.???外部程序访问我们的程序,访问的实际是Service层???11:42 ? ? 8. ??? 1.找到查询手机归属地的说明书 ?????? 说明书中的<soap12..>或<soap>可以查看soap的版本 ?????? wsimport只能编译1.1 ??? 2.将说明书保存为phone.wsdl ??? 3.使用wsimport生成.java源文件 ??? (wsimport -s .http://192.168....) ??? wsimport -s .F:phone.wsdl ??? 4.新建一个工程,将生成.java文件复制到工程中 ??? 5.工程中新建Main.java,里面新建main方法,方法中 ??? ??? <wsdl:servicename="MobileCodeWS"..="">?? ==>查看类名 ??? 调用: ??? MobileCodeWSSoapmobileCodeWSSoap = new MobileCodeWS().getMobileCodeWSSoap(); ??? ??? <wsdl:operationname="getMobileCodeInfo"...>? ===>查看方法名 ??? 调用: ??? mobileCodeWSSoap.getMobileCodeInfo("15801551555",userId); ??? ? ? ? ====================== API javax.xml.ws.Endpoint类==>Web 服务端点。 ??? abstract? void publish(Object serverContext) ?????? ? 在提供的服务器上下文中发布此端点。 ??? abstract? void publish(String address) ?????? ? 在给定地址处发布此端点。 ??? static Endpointpublish(String address,Object implementor) ?????? ? 在给定地址处针对指定的实现者对象创建并发布端点。 javax.jws.@WebService注解==>将 Java 类标记为实现Web Service,或者将 Java 接口标记为定义Web Service 接口。 ??? StringendpointInterface ?????? ? 定义服务抽象 Web Service 协定的服务端点接口的完整名称。 ??? String name ?????? ? Web Service 的名称。 ??? String portName ?????? ? Web Service 的端口名称。 ??? String serviceName ?????? ? Web Service 的服务名称。 ??? String targetNamespace ?????? ? 如果 @WebService.targetNamespace 注释是关于某一服务端点接口的,则 targetNamespace 用于 wsdl:portType(以及关联的 XML 元素)的名称空间。 ??? String wsdlLocation ?????? ? 描述服务的预定义 WSDL 的位置 ? 面试的时候会遇到的问题: 1、webservice就是在各个独立的应用程序之间架起了通讯的桥梁。 ? 2、webservice的内部实现原理 WebService实质就是通过HTTP协议发格式的XML格式。我们将这种格式的XML数据叫做SOPA. ? webservice的框架有: CXF,axis,axis2,xfire (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 如何利用angular-cli组织项目结构
- Angular2 RC:在创建ControlGroup后,使用addCont
- AppleScript可以输出shell脚本并放入粘贴缓冲区吗
- angular – 与Select / Options和ngModel / ngMo
- AngularJS自定义验证在程序更改模型时不会触发
- bash – 添加用户脚本不工作,正在运行:read $us
- angularjs – TeamCity:PhantomJS还没有在6万毫
- 英文系统启动中文Netbeans 命令行
- angular – 由typescript导入的代码的Typescript
- 如何将.bashrc导出到.zshrc?