java中java写webservice接口口实现的过程是怎么样一个过程,现在就知道调用参数和返回数据,是自己放一个XML的?

&&完如何在java Web项目中开发WebService接口之前没怎么接触过WebService,能不能提供点详细的步骤呀!求大神们指导3个牛币所有回答列表(8)&LV2花点时间,用MyEclipse给你写一下步骤吧1、新建立一个javaWeb项目,一个java类,如图:2、接下来我们就要将项目中的TestService的这个类生成WebService服务端,选择new Web Service,如图:Next,选择java文件,如图:Finish。你会看见项目中会自动生成一些文件,好,下面我们来启动项目,你会发现报异常,如图:接下来我们要在项目中加入JAX-RS。具体有哪些我忘记了。我这里就全部引入了。如图:点击filish,然后重新编译,启动项目,你会发现项目已经正常启动,OK,我们的webService服务端已完成,访问以下试试看,如图:开会,回来继续写 Client端。好,我们继续,下面我们新建一个java项目来测试webService接口,项目创建完毕后选择new Web Service Client 如图:选择java项目,next,如图:WSDL URL为 webservice服务的访问呢地址,Client文件将会生成在java package下,如图:Next -- Next -- Finish。你会看到java package目录中生成一些文件。此时客户端已完成。我们来新建一个类来测试接口,如图:PS:有错别字,抱歉哈....好了,就到这里吧! & & -- Coffee_冬孓最佳答案2、接下来我们就要将项目中的TestService的这个类生成WebService服务端,选择new Web Service,如图:不明确啊,在哪选择啊上电视了jersey &rest风格webservice &配置简单&不错 很好 值得 学习我的为什么访问不到页面啊,大神,我用的是myeclipse8.5可以 ,对于才接触的人来说相对于来说还算详细&非常感谢等完等等等等等等等等等等相关问答等完完等完等等完等完等等最近浏览暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级扫描二维码关注最代码为好友"/>扫描二维码关注最代码为好友WebService在java中的调用_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
WebService在java中的调用
&&介绍如何在java中调用WebService
阅读已结束,下载文档到电脑
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩2页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢10:54 提问
webservice通过url传递参数调用接口,参数中含“/”要怎么处理?
我现在在做一个调用企业QQ接口来发信息的平台,是通过webservice在url传递参数调用接口。。
但是短信内容中含斜杠时会干扰参数的传递,大神教我怎么解决啊?
@Path("/sendTips/{receivers}/{content}/{windowTitle}/{tipsTitle}/{tipsUrl}")
@Produces ({MediaType.APPLICATION_JSON})
public String sendTips(@PathParam("receivers") String receivers, @PathParam("content") String content,
@PathParam("windowsTitle") String windowsTitle, @PathParam("tipsTitle") String tipsTitle,
@PathParam("tipsUrl") String tipsUrl) throws SystemE
按赞数排序
建议url 转义(URLEncoder类)即可,Java里面有方法
可以使用Unicode 编码啊
用URLEncoder进行编码,然后用再用URLDecoder进行解码
可以规定是UTF-8或者GBK之类的 建议UTF-8
其他相关推荐博客分类:
使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService。下面是一个从编写测试例子到发布WebService,以及编写测试代码的过程介绍。
本例子的WebService提供了两个方法,分别是sayHello和sayHelloToPerson,第一个只是返回一个"Hello"字符串,没有参数,第二个函数接受一个字符串作为参数,返回"Hello 参数值",该例子比较简单,但是清楚的说明了从编写代码到发布为WebService以及测试编写好的WebService全过程。
编写服务代码
服务代码提供了两个函数,分别为sayHello和sayHelloToPerson,源代码如下:
* File name: HelloService.java
* Version: v1.0
* Created on Aug 2, :20 AM
* Designed by Stephen
* (c)Copyright 2008
package com.sinosoft.
* @author Stephen
* Test web service
public class HelloService {
* 不带参数的函数
* @return 返回Hello字符串
public String sayHello() {
return "Hello";
* 带参数的函数
* @param name
* @return 返回加上名称的欢迎词
public String sayHelloToPerson(String name) {
if (name == null || name.equals("")) {
name = "nobody";
return "Hello " +
发布WebService
要将上边写的HelloService类发布为WebService,需要先搭建Web应用。下面是在Tomcat下使用Axis()创建WebService服务的例子。
在Tomcat下创建Web应用
在该例子中,在Tomcat下创建了一个context path为ws的WEB应用。
1. 在myeclipse中创建WebServiceTest的webproject
2. 在WEB-INF文件夹下web.xml文件,该文件的内容如下:
&?xml version="1.0" encoding="ISO-8859-1"?&
&!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "/dtd/web-app_2_3.dtd"&
&display-name&Apache-Axis&/display-name&
&listener&
&listener-class&org.apache.axis.transport.http.AxisHTTPSessionListener&/listener-class&
&/listener&
&servlet-name&AxisServlet&/servlet-name&
&display-name&Apache-Axis Servlet&/display-name&
&servlet-class&
org.apache.axis.transport.http.AxisServlet
&/servlet-class&
&/servlet&
&servlet-name&AdminServlet&/servlet-name&
&display-name&Axis Admin Servlet&/display-name&
&servlet-class&
org.apache.axis.transport.http.AdminServlet
&/servlet-class&
&load-on-startup&100&/load-on-startup&
&/servlet&
&servlet-name&SOAPMonitorService&/servlet-name&
&display-name&SOAPMonitorService&/display-name&
&servlet-class&
org.apache.axis.monitor.SOAPMonitorService
&/servlet-class&
&init-param&
&param-name&SOAPMonitorPort&/param-name&
&param-value&5001&/param-value&
&/init-param&
&load-on-startup&100&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&AxisServlet&/servlet-name&
&url-pattern&/servlet/AxisServlet&/url-pattern&
&/servlet-mapping&
&servlet-mapping&
&servlet-name&AxisServlet&/servlet-name&
&url-pattern&*.jws&/url-pattern&
&/servlet-mapping&
&servlet-mapping&
&servlet-name&AxisServlet&/servlet-name&
&url-pattern&/services/*&/url-pattern&
&/servlet-mapping&
&servlet-mapping&
&servlet-name&SOAPMonitorService&/servlet-name&
&url-pattern&/SOAPMonitor&/url-pattern&
&/servlet-mapping&
&!-- uncomment this if you want the admin servlet --&
&servlet-mapping&
&servlet-name&AdminServlet&/servlet-name&
&url-pattern&/servlet/AdminServlet&/url-pattern&
&/servlet-mapping&
&session-config&
&!-- Default to 5 minute session timeouts --&
&session-timeout&5&/session-timeout&
&/session-config&
&!-- currently the W3C havent settled on a media type for WSDL;
http://www.w3.org/TR/2003/WD-wsdl12-/#ietf-draft
for now we go with the basic 'it's XML' response --&
&mime-mapping&
&extension&wsdl&/extension&
&mime-type&text/xml&/mime-type&
&/mime-mapping&
&mime-mapping&
&extension&xsd&/extension&
&mime-type&text/xml&/mime-type&
&/mime-mapping&
&welcome-file-list id="WelcomeFileList"&
&welcome-file&index.jsp&/welcome-file&
&welcome-file&index.html&/welcome-file&
&welcome-file&index.jws&/welcome-file&
&/welcome-file-list&
&/web-app&
在上面的web.xml中主要是配置了axis的相关配置。
axis的相关配置
在上述的web.xml文件中已经对axis进行了配置,但是还需要进行额外的配置。
复制axis相关的jar文件
将axis的相关jar文件复制到WEB-INF\lib文件夹下。这些文件包括:
activation.jaraxis.jaraxis-ant.jarcommons-discovery-0.2.jarcommons-logging-1.0.4.jarjaxrpc.jarlog4j-1.2.8.jarsaaj.jarwsdl4j-1.5.1.jar
测试发布的Web应用
启动Tomcat服务,打开IE浏览器,访问地址,如果看到如下界面就说明AXIS部署成功了。
发布WebService
发布WebService需要使用现有的AdminService来实现,这里我写了一个批处理文件来发布WebService,以后如果需要发布其他文件,只需要修改相应的参数就可以了。
创建deploy.wsdd文件
文件deploy.wsdd内容如下所示:
&?xml version="1.0" encoding="UTF-8"?&
&deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"&
&service name="HelloServices" provider="java:RPC"&
&parameter name="className" value="com.sinosoft.webservice.HelloService"/&
&parameter name="allowedMethods" value="*"/&
&/service&
&/deployment&
创建发布WebService服务的批处理文件
批处理文件deploywebservice.bat内容如下:
java -cp axis-ant.axis-schema.axis.commons-discovery-0.2.commons-logging-1.0.4.jaxrpc.log4j-1.2.8.saaj.wsdl4j-1.5.1.xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd
其中上面的jar包我都拷到和bat文件在同一个目录,现在将所有的jar文件都加入到classpath中进行执行。
-l后的参数是本地要发布WebService的AdminService对应的访问地址。
最后deploy.wsdd是对应的配置文件名称。
发布WebService服务
将deploy.wsdd文件和deploywebservice.bat文件复制到同一个文件夹下,执行deploywebservice.bat批处理文件,就可以将deploy.wsdd中描述的Java类发布为WebService。发布完成之后在访问如下图所示:
从上图可以看出,发布成功后,多了一个HelloServices的服务。这样就说明HelloService发布成功了。
查看HelloServices的wsdl
访问可以看到如下wsdl的内容:
&?xml version="1.0" encoding="UTF-8"?&
&wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&
&!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, :48 PDT)--&
&wsdl:message name="sayHelloToPersonRequest"&
&wsdl:part name="name" type="soapenc:string"/&
&/wsdl:message&
&wsdl:message name="sayHelloRequest"&
&/wsdl:message&
&wsdl:message name="sayHelloToPersonResponse"&
&wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/&
&/wsdl:message&
&wsdl:message name="sayHelloResponse"&
&wsdl:part name="sayHelloReturn" type="soapenc:string"/&
&/wsdl:message&
&wsdl:portType name="HelloService"&
&wsdl:operation name="sayHello"&
&wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/&
&wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/&
&/wsdl:operation&
&wsdl:operation name="sayHelloToPerson" parameterOrder="name"&
&wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/&
&wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/&
&/wsdl:operation&
&/wsdl:portType&
&wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService"&
&wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/&
&wsdl:operation name="sayHello"&
&wsdlsoap:operation soapAction=""/&
&wsdl:input name="sayHelloRequest"&
&wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="" use="encoded"/&
&/wsdl:input&
&wsdl:output name="sayHelloResponse"&
&wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/&
&/wsdl:output&
&/wsdl:operation&
&wsdl:operation name="sayHelloToPerson"&
&wsdlsoap:operation soapAction=""/&
&wsdl:input name="sayHelloToPersonRequest"&
&wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="" use="encoded"/&
&/wsdl:input&
&wsdl:output name="sayHelloToPersonResponse"&
&wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/&
&/wsdl:output&
&/wsdl:operation&
&/wsdl:binding&
&wsdl:service name="HelloServiceService"&
&wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices"&
&wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/&
&/wsdl:port&
&/wsdl:service&
&/wsdl:definitions&
用Java调用WebService实例
下面是用Java调用刚发布的WebService例子。
* File name: TestHelloService.java
* Version: v1.0
* Created on Aug 2, :10 AM
* Designed by Stephen
* (c)Copyright 2008
.sinosoft.
import java.io.IOE
import java.net.MalformedURLE
import javax.xml.namespace.QN
import javax.xml.rpc.ServiceE
import org.apache.axis.client.C
import org.apache.axis.client.S
import mons.logging.L
import mons.logging.LogF
* @author Stephen
* 测试调用WebService
public class TestHelloService {
private static final Log log = LogFactory.getLog(TestHelloService.class);
private static final String HELLO_SERVICE_ENDPOINT = "http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl";
public static void main(String[] args) {
TestHelloService tester = new TestHelloService();
// tester.callSayHello();
tester.callSayHelloToPerson();
public void callSayHello() {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(
HELLO_SERVICE_ENDPOINT));
//下面名字查询的http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl文件里有
call.setOperationName(new QName("/",
"sayHello"));
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
//远程调用发布的方法
String ret = (String) call.invoke(new Object[] {});
System.out.println("The return value is:" + ret);
} catch (IOException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
log.error("call sayHello service error!");
public void callSayHelloToPerson() {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT));
call.setOperationName(new QName("/",
"sayHelloToPerson"));
call.addParameter("name", org.apache.axis.Constants.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
String ret = (String) call.invoke(new Object[] { "Stephen" });
System.out.println("The return value is:" + ret);
} catch (IOException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
log.error("call sayHello service error!");
浏览 79482
hz_chenwenbiao
浏览: 678708 次
来自: 广州
能够随便也发一下,你退火算法程序使用的DistanceMatr ...
感谢分享。。帮大忙了
openoffice和文件不在同一个服务器上,用过吗?
如何在java Web项目中开发WebService接口htt ...
唉,看起来好像很详细很不错的样子,可惜不是篇面向初学者的文章, ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'jaxb JAVA的 WebService规范 JAX-WS实现例子 - 为程序员服务
为程序员服务
JAVA的 WebService规范 JAX-WS实现例子
Web 服务分为Server、Client 两部分,Server 公开Web 服务,Client 调用Web 服务,JAX-WS 的服务端、客户端双方传输数据使用的SOAP 消息格式封装数据,在后面我们会看到其实SOAP 信封内包装的就是一段XML 代码.
I.服务端示例:
我们先看一个服务器端示例:
(1)公开Web 服务的接口IHelloService:
package cn.outofmemory.JASWS;
import javax.jws.WebS
@WebService
public interface IHelloService {
Customer selectMaxAgeStudent(Customer c1, Customer c2);
Customer selectMaxLongNameStudent(Customer c1, Customer c2);
我们看到这个接口很简单,仅仅是使用类级别注解@WebService 就标注了这个接口的方法将公开为Web 服务,使用了这个注解的接口的所有方法都将公开为Web 服务的操作,如果你想屏蔽某个方法,可以使用方法注解@Method 的exclude=true.我们也通常把公开为Web服务的接口叫做SEI(Service EndPoint Interface)服务端点接口.
(2)实现类HelloServiceImpl:
package cn.outofmemory.JASWS;
import javax.jws.WebS
@WebService
public class HelloServiceImpl implements IHelloService {
public Customer selectMaxAgeStudent(Customer c1, Customer c2) {
if (c1.getBirthday().getTime() & c2.getBirthday().getTime())
return c2;
return c1;
public Customer selectMaxLongNameStudent(Customer c1, Customer c2)
if (c1.getName().length() & c2.getName().length())
return c1;
return c2;
这个实现类没有任何特殊之处,但是如果你的实现类还实现了其他的接口,那么你需要在实现类上使用@WebService 注解的endpointInterface 属性指定那个接口是SEI(全类名).
(3)Customer 类:
package cn.outofmemory.JASWS;
import java.util.D
import javax.xml.bind.annotation.XmlRootE
@XmlRootElement(name = &Customer&)
public class Customer {
public long getId() {
public void setId(long id) {
public String getName() {
public void setName(String name) {
this.name =
public Date getBirthday() {
public void setBirthday(Date birthday) {
this.birthday =
这个类是公开为Web 服务的接口中的参数类型和返回值,因此你需要使用JAXB 注解告诉CXF 如何在XML和Java Object 之间处理,因为前面说过SOAP 消息格式包装的是一段XML代码,那么无论是服务器端还是客户端在接收到SOAP 消息时都需要将XML 转化为JavaObject,在发送SOAP 消息时需要将Java Object 转化为XML.
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档.从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数.
JDK中JAXB相关的重要Annotation:
@XmlType,将Java类或枚举类型映射到XML模式类型
@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化.FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标注)字段到XML.其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE.
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序.
@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML.
@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器).
@XmlRootElement,将Java类或枚举类型映射到XML元素.
@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素.
@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性.
对于要序列化(marshal)为XML的Java类,绝不能把成员变量声明为public,否则运行将抛出异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException.
对于JAXB相关的重要Annotation的声明,如@Xml……,可以放在成员变量的setter()或getter()方法上,两者中任选其一即可,但决不能放在成员变量上,否则运行将抛出异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException.
(4)发布Web 服务:
package cn.outofmemory.JASWS;
import javax.xml.ws.E
public class SoapServer {
public static void main(String[] args) {
Endpoint.publish(&http://127.0.0.1:8080/helloService&,new HelloServiceImpl());
注意我们发布Web 服务使用的是javax.xml.ws.*包中的EndPoint 的静态方法publish().
(5)查看WSDL:
我们访问http://127.0.0.1:8080/helloService?wsdl 地址,您会看到很长的XML 文件(由于浏览器的问题,如果你看到的是空白页面,请查看源代码),这就是WSDL(WebService DefinitionLanguage),对于你要访问的Web 服务,只要在其地址后加上,就可以在浏览器中查看用于描述Web 服务的WSDL,这也是一种XML,Web 服务能够被各种编程语言书写的程序访问就是通过WSDL 这种通用的契约来完成的.如果你已经看到WSDL,那么表示我们的Web 服务发布成功了.你可能会差异,我们没有
借助Tomcat 这样的Web 服务器,直接运行一个main 方法是怎么发布的Web 服务呢?其实CXF 内置了Jetty(Servlet 容器),因此你不需要将你的程序部署到Tomcat 等Web 服务器也可以正常发布Web 服务.
(6)返回的WSDL:
&?xml version=&1.0& encoding=&UTF-8&?&
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-&definitions xmlns:wsu=&http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd& xmlns:wsp=&http://www.w3.org/ns/ws-policy& xmlns:wsp1_2=&http://schemas.xmlsoap.org/ws/2004/09/policy& xmlns:wsam=&http://www.w3.org/2007/05/addressing/metadata& xmlns:soap=&http://schemas.xmlsoap.org/wsdl/soap/& xmlns:tns=&http://JASWS/& xmlns:xsd=&http://www.w3.org/2001/XMLSchema& xmlns=&http://schemas.xmlsoap.org/wsdl/& targetNamespace=&/JASWS/& name=&HelloServiceImplService&&
-&xsd:schema&
&xsd:import namespace=&/JASWS/& schemaLocation=&http://127.0.0.1:8080/helloService?xsd=1& /&
&/xsd:schema&
-&message name=&selectMaxLongNameStudent&&
&part name=&parameters& element=&tns:selectMaxLongNameStudent& /&
&/message&
-&message name=&selectMaxLongNameStudentResponse&&
&part name=&parameters& element=&tns:selectMaxLongNameStudentResponse& /&
&/message&
-&message name=&selectMaxAgeStudent&&
&part name=&parameters& element=&tns:selectMaxAgeStudent& /&
&/message&
-&message name=&selectMaxAgeStudentResponse&&
&part name=&parameters& element=&tns:selectMaxAgeStudentResponse& /&
&/message&
-&portType name=&HelloServiceImpl&&
-&operation name=&selectMaxLongNameStudent&&
&input wsam:Action=&http://JASWS/HelloServiceImpl/selectMaxLongNameStudentRequest& message=&tns:selectMaxLongNameStudent& /&
&output wsam:Action=&http://JASWS/HelloServiceImpl/selectMaxLongNameStudentResponse& message=&tns:selectMaxLongNameStudentResponse& /&
&/operation&
-&operation name=&selectMaxAgeStudent&&
&input wsam:Action=&http://JASWS/HelloServiceImpl/selectMaxAgeStudentRequest& message=&tns:selectMaxAgeStudent& /&
&output wsam:Action=&http://JASWS/HelloServiceImpl/selectMaxAgeStudentResponse& message=&tns:selectMaxAgeStudentResponse& /&
&/operation&
&/portType&
-&binding name=&HelloServiceImplPortBinding& type=&tns:HelloServiceImpl&&
&soap:binding transport=&http://schemas.xmlsoap.org/soap/http& style=&document& /&
-&operation name=&selectMaxLongNameStudent&&
&soap:operation soapAction=&& /&
&soap:body use=&literal& /&
&soap:body use=&literal& /&
&/operation&
-&operation name=&selectMaxAgeStudent&&
&soap:operation soapAction=&& /&
&soap:body use=&literal& /&
&soap:body use=&literal& /&
&/operation&
&/binding&
-&service name=&HelloServiceImplService&&
-&port name=&HelloServiceImplPort& binding=&tns:HelloServiceImplPortBinding&&
&soap:address location=&http://127.0.0.1:8080/helloService& /&
&/service&
&/definitions&
下一节来分析该WSDL的结构和组成元素.
客户端调用程序
我们从上面可以知道Web 服务只向客户端暴漏WSDL,那么客户端必须将WSDL 转换为自己的编程语言书写的代码.JAX-WS 的各种实现都提供相应的工具进行WSDL 与JAVA 之间的互相转换,你可以在CXF 的运行包中找到bin 目录,其中的wsdl2java.bat 可以将WSDL转换为JAVA 类,bin 目录的各种bat 的名字可以很容易知道其作用,但要注意JAVA 类转换为WSDL 最好使用前面的URL?wsdl 的方式获得,因为这样得到的是最准确的.你可以在命令行将当前目录切换到CXF 的bin 目录,然后运行wsdl2java –h 查看这个批处理命令的各个参数的作用,常用的方式就是 wsdljava –p 包路径 –d 目标文件夹 wsdl 的 url地址.现在我们将前面的WSDL生成客户端代码:
wsdl2java -p net.ilkj.soap.client –d E:\ http://127.0.0.1:8080/helloService?wsdl
你会在E 盘根目录找到生成的客户端代码,然后将它复制到Eclipse 工程即可使用.
如果你使用MyEclipse,可以按照如下步骤从WSDL 生成客户端代码:
New---&Other---&MyEclipse---&Web Services---&Web Services Client,然后依据设置向导即可完成,但最好还是使用CXF 的wsdl2java 来完成,因为CXF2.2+版本开始支持JAX-WS2.1规范,而MyEclipse 自带的好像是XFire 的wsdl2java,生成的客户端代码可能不是最新规范的.
我们上面的WSDL 会生成如下所示的客户端代码:
Customer.java
HelloServiceImplService.java
IHelloService.java
ObjectFactory.java
package-info.java
SelectMaxAgeStudent.java
SelectMaxAgeStudentResponse.java
SelectMaxLongNameStudent.java
SelectMaxLongNameStudentResponse.java
其中package-info.java、ObjectFactory.java 是JAXB需要的文件;HelloServiceImplService.java继承自javax.xml.ws.Service 类,用于提供WSDL 的客户端视图,里面使用的是大量javax.xml.ws.*包中的注解;剩下的类是Web 服务的接口、方法参数、响应值的类.
在 CXF 中使用JaxWsProxyFactoryBean 客户端代理工厂调用Web 服务,代码如下所示:
package cn.outofmemory.JASWS;
import java.text.ParseE
import java.text.SimpleDateF
import java.util.D
import java.util.GregorianC
import org.apache.cxf.jaxws.JaxWsProxyFactoryB
public class SoapClient {
public static void main(String[] args) throws ParseException {
JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress(&http://127.0.0.1:8080/helloService&);
soapFactoryBean.setServiceClass(IHelloService.class);
Object o = soapFactoryBean.create();
IHelloService helloService = (IHelloService)
Customer c1 = new Customer();
c1.setId(1);
c1.setName(&A&);
GregorianCalendar calendar = (GregorianCalendar)
GregorianCalendar
.getInstance();
calendar.setTime(new SimpleDateFormat(&yyyy-MM-dd&).parse(&&));
c1.setBirthday(new Date(&&));
Customer c2 = new Customer();
c2.setId(2);
c2.setName(&B&);
calendar.setTime(new SimpleDateFormat(&yyyy-MM-dd&).parse(&&));
c2.setBirthday(new Date(&&));
System.out.println(helloService.selectMaxAgeStudent(c1,c2).getName());
您可能的代码
相关聚客文章
荣誉:2088
相关专栏文章

我要回帖

更多关于 实现webservice接口 的文章

 

随机推荐