java调用webservicejava 怎样调用本地webservice

java调用webservice  时间:2021-06-02  阅读:()

java 如何实现webservice 怎么调用接口

一、利用jdk?web服务api实现,这里使用基于SOAP?message的Web服务   ①.首先建立一个Web?services?EndPoint:package?Hello;  import?javax.jws.WebService;  import?javax.jws.WebMethod;  import?javax.xml.ws.Endpoint;   @WebService  public?class?Hello?{   @WebMethod  public?String?hello(String?name)?{  return?"Hello,?"?+?name?+?" ";  }   public?static?void?main(String[]?args)?{   //?create?and?publish?an?endpoint   Hello?hello?=?new?Hello(); Endpoint?endpoint?=?Endpoint.publish(" ,?hello);  }  }  ②.使用apt编译Hello.java(例:apt?-d?[存放编译后的文件目录]?Hello.java?)?, 会生成jaws目录  ③.使用java?Hello.Hello运行,然后将浏览器指向 就会出现下列显示  ④.使用wsimport生成客户端使用如下:   wsimport?-p?.?-keep? 这时,会在当前目录中生成如下文件:  ⑤.客户端程序: 1?class?HelloClient{ 2?public?static?void?main(String?args[])?{ 3?HelloService?service?=?new?HelloService(); 4?Hello?helloProxy?=?service.getHelloPort(); 5?String?hello?=?helloProxy.hello("你好"); 6?System.out.println(hello); 7?} 8?}以上方法还稍显繁琐,还有更加简单的方法   二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的利用xfire开发WebService,可以有三种方法: ? ?1. 一种是从javabean中生成;  2.?一种是从wsdl文件中生成; ? ?3. 还有一种是自己建立webservice   步骤如下:   用myeclipse建立webservice工程,目录结构如下:首先建立webservice接口, 代码如下: ????1?.myeclipse.wsExample;   2?//Generated?by?MyEclipse   3   4?public?interface?IHelloWorldService?{   5 ????6?public?String?example(String?message);   7   8?} 接着实现这个借口: ????1?.myeclipse.wsExample;   2?//Generated?by?MyEclipse   3   4?public?class?HelloWorldServiceImpl?implements?IHelloWorldService?{   5   6?public?String?example(String?message)?{   7?return?message;   8?}   9   10?}   修改 service.xml文件,加入以下代码:   1?<service>   2?<name>HelloWorldService</name>   3?<serviceClass>   .myeclipse.wsExample.IHelloWorldService   5?</serviceClass>   6?<implementationClass>   .myeclipse.wsExample.HelloWorldServiceImpl   8?</implementationClass>   9?<?style>wrapped</style>   10?<use>literal</use>   11?<scope>application</scope>   12  </service> 把整个项目部署到tomcat服务器中打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下: 然后再展开HelloWorldService后面的wsdl可以看到:  客户端实现如下:  ??1?.myeclipse.wsExample.client;   2   3?import?.MalformedURLException;   4?import?.URL;   5   6?.codehaus.xfire.XFireFactory;   7?.codehaus.xfire.client.Client;   8?.codehaus.xfire.client.XFireProxyFactory;   9?.codehaus.xfire.service.Service;  ??10?.codehaus.xfire.service.binding.ObjectServiceFactory;   11   12?.myeclipse.wsExample.IHelloWorldService;   13   14?public?class?HelloWorldClient?{   15?public?static?void?main(String[]?args)?throws?MalformedURLException,?Exception?{   16?//?TODO?Auto-generated?method?stub   17?Service?s=new?ObjectServiceFactory().create(IHelloWorldService.class);   18?XFireProxyFactory?xf=new?XFireProxyFactory(XFireFactory.newInstance().getXFire());   19?String?url="   20   21?try   22?{   23 ????24?IHelloWorldService?hs=(IHelloWorldService)?xf.create(s,url);   25?String?st=hs.example("zhangjin");     26?System.out.print(st);   27?}     28?catch(Exception?e)   29?{   30?e.printStackTrace();   31?}   32?}   33   34?}? ??有时候我们知道一个wsdl地址,比如想用java客户端引做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下: 1.?public?static?void?main(String[]?args)?throws?MalformedURLException,?Exception?{ 2.?//?TODO?Auto-generated?method?stub

Java中怎样口述 调用webservice接口(比如订单,天气),还有 树形结构菜单怎样口述

建立个普通的web工程就可以 服务器端首先需要导入对应的包,在web-inf下面建立server-config.wsdd,具体里面的内容大致如下 <ns1:deployment xmlns="/axis/wsdd/" xmlns:java="/axis/wsdd/providers/java" xmlns:ns1="/axis/wsdd/"> <ns1:globalConfiguration> <ns1:parameter name="sendMultiRefs" value="true"/> <ns1:parameter name="disablePrettyXML" value="true"/> <ns1:parameter name="adminPassword" value="admin"/> <ns1:parameter name="attachments.Directory" value=""/> <ns1:parameter name="dotNetSoapEncFix" value="true"/> <ns1:parameter name="enableNamespacePrefixOptimization" value="false"/> <ns1:parameter name="sendXMLDeclaration" value="true"/> <ns1:parameter name="sendXsiTypes" value="true"/> <ns1:parameter name="attachments.implementation" value=.apache.axis.attachments.AttachmentsImpl"/> <ns1:requestFlow> <ns1:handler type=".apache.axis.handlers.JWSHandler"> <ns1:parameter name="scope" value="session"/> </ns1:handler> <ns1:handler type=".apache.axis.handlers.JWSHandler"> <ns1:parameter name="scope" value="request"/> <ns1:parameter name="extension" value=".jwr"/> </ns1:handler> </ns1:requestFlow> </ns1:globalConfiguration> <ns1:handler name="URLMapper" type=".apache.axis.handlers.http.URLMapper"/> <ns1:handler name="LocalResponder" type=".apache.axis.transport.local.LocalResponder"/> <ns1:handler name="Authenticate" type=".apache.axis.handlers.SimpleAuthenticationHandler"/> <ns1:service name="AdminService" provider="java:MSG"> <ns1:parameter name="allowedMethods" value="AdminService"/> <ns1:parameter name="enableRemoteAdmin" value="false"/> <ns1:parameter name="className" value=.apache.axis.utils.Admin"/> <ns1:namespace>/axis/wsdd/</ns1:namespace> </ns1:service> <ns1:service name="Version" provider="java:RPC"> <ns1:parameter name="allowedMethods" value="getVersion"/> <ns1:parameter name="className" value=.apache.axis.Version"/> </ns1:service> <ns1:operation name="getTest" qname="ns1:getTest" returnQName="ns1:getTestReturn" returnType="xsd:string" soapAction="" xmlns:ns1="" xmlns:xsd="/2001/XMLSchema" xmlns:ns2="/axis/wsdd/"> <ns1:parameter qname="ns1:filetype" type="xsd:string"/> </ns1:operation> <ns1:parameter name="allowedMethods" value="*"/> <ns1:parameter name="typeMappingVersion" value="1.2"/> <ns1:parameter name="wsdlPortType" value="TestWebService"/> <ns1:parameter name="className" value=.test.web.TestWebService"/> <ns1:parameter name="wsdlServicePort" value="TestWebService"/> <ns1:parameter name="schemaQualified" value=""/> <ns1:parameter name="wsdlTargetNamespace" value=""/> <ns1:parameter name="wsdlServiceElement" value="TestWebServiceService"/> </ns1:service> <ns1:transport name="http"> <ns1:requestFlow> <ns1:handler type="URLMapper"/> <ns1:handler type=".apache.axis.handlers.http.HTTPAuthHandler"/> </ns1:requestFlow> <ns1:parameter name="qs:list" value=.apache.axis.transport.http.QSListHandler"/> <ns1:parameter name="qs:wsdl" value=.apache.axis.transport.http.QSWSDLHandler"/> <ns1:parameter name="qs.list" value=.apache.axis.transport.http.QSListHandler"/> <ns1:parameter name="qs.method" value=.apache.axis.transport.http.QSMethodHandler"/> <ns1:parameter name="qs:method" value=.apache.axis.transport.http.QSMethodHandler"/> <ns1:parameter name="qs.wsdl" value=.apache.axis.transport.http.QSWSDLHandler"/> </ns1:transport> <ns1:transport name="local"> <ns1:responseFlow> <ns1:handler type="LocalResponder"/> </ns1:responseFlow> </ns1:transport> </ns1:deployment> 里面定义了要调用的类的名称路径及其中方法,传入参数等 提供的类就是个普通的类文件 TestWebService { public String getTest(String filetype){ return "test"; } } 调用的类访问接口类似 public void getStr(){ try { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new .URL("http://localhost:8090/test/services/TestWebService?wsdl")); call.setOperationName(new QName("", "getTestReturn")); call.addParameter("filetype", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN); .apache.axis.Constants.XSD_STRING); try { String ret = (String) call .invoke(new Object[] { "1",}); List list= xmlToList(ret); for(int i=0;i<list.size();i++){ System.out.println( ((String[])list.get(i))[0]); System.out.println( ((String[])list.get(i))[1]); } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } } 上述代码不保证能正确运行,是从能运行的版本修改的,最好再参照相应的资料进行理解

java如何调用webservice接口

简单来说就是你编写的webservice 是要有xml文件来进行配置的 你的接口名 方法名 这样就能向外发布接口,外面的接口也是通过配置你电脑或是服务器的ip地址加上你的接口名称就能调用你的webservice接口了。

java 怎样调用本地webservice

WebService是基于Web的服务,WebService使用SOAP协议实现跨编程语言和跨操作系统平台,接收和响应外部系统的某种请求,从而实现远程调用。

WebService采用HTTP协议传输数据,采用XML格式封装数据,SOAP协议=HTTP协议+XML数据格式。

主要解决不了不同的系统或者调用分布部署的处理数据项目返回的接口。

第一种,采用httpclient请求,这种跟经常用的HTTP请求一样,结果可以是返回XML格式的字符串,比较容易对其进行解析,取得想要的数据。

#消息# contabo:德国老牌机房新增美国“纽约、西雅图”数据中心,免设置费

运作了18年的德国老牌机房contabo在继去年4月开办了第一个美国数据中心(中部城市:圣路易斯)后立马在本月全新上马两个数据中心:纽约、西雅图。当前,为庆祝美国独立日,美国三个数据中心的VPS全部免除设置费,VPS本身的配置很高,价格适中,有较高的性价比!官方网站:https://contabo.com/en/SSD VPSKVM虚拟,纯SSD阵列,不限制流量,自带一个IPv4内存CPUSSD带...

SugarHosts糖果主机商更换域名

昨天,遇到一个网友客户告知他的网站无法访问需要帮他检查到底是什么问题。这个同学的网站是我帮他搭建的,于是我先PING看到他的网站是不通的,开始以为是服务器是不是出现故障导致无法打开的。检查到他的服务器是有放在SugarHosts糖果主机商中,于是我登录他的糖果主机后台看到服务器是正常运行的。但是,我看到面板中的IP地址居然是和他网站解析的IP地址不同。看来官方是有更换域名。于是我就问 客服到底是什...

TMTHosting:夏季优惠,美国西雅图VPS月付7折,年付65折,美国服务器95折AS4837线路

tmthosting怎么样?tmthosting家本站也分享过多次,之前也是不温不火的商家,加上商家的价格略贵,之到斯巴达商家出现,这个商家才被中国用户熟知,原因就是斯巴达家的机器是三网回程AS4837线路,而且也没有多余的加价,斯巴达家断货后,有朋友发现TMTHosting竟然也在同一机房,所以大家就都入手了TMTHosting家的机器。目前,TMTHosting商家放出了夏季优惠,针对VPS推...

java调用webservice为你推荐
宝应中学宝应县有哪些高中iso20000认证什么是ISO20000-IT服务管理体系cpu监控电脑硬件监控软件有哪些?知识分享平台关于分享职场技能的知识付费平台,大家有什么好推荐的吗?视频技术怎么做视频?华为总裁女儿为啥姓孟总裁文女主姓孟,女主父母抱错孩子,后来将错就错,养父母对女主很好12种颜色水粉颜料调色过程十二种颜色腾讯技术腾讯是什么东西?什么是生态系统生态系统的功能有什么?单元测试规范求解,单片机程序的单元测试应该怎么做呢?
手机网站空间 泛域名 最便宜的vps 申请免费域名 187邮箱 duniu 站群服务器 老左博客 免费mysql qq对话框 河南移动网 hdd 网站加速软件 百度云加速 英国伦敦 主机管理系统 独立主机 个人免费邮箱 日本代理ip 永久免费空间 更多