首页 > 编程知识 正文

webservice接口调用(webservice接口实例)

时间:2023-05-05 19:51:01 阅读:74192 作者:4342

最近正在学习web服务,今天试图使用Eclipse插件生成jax-ws web服务。 结果,发生了很多问题呢。 调试了半天终于打通了程序。 现在记录步骤和问题,以便以后遇到同样的问题时能及时解决。 首先,利用Eclipse生成web服务的服务端。

1、创建名为“ws-server”的web项目dynamicwebproject (文件新动态webproject )

1、创建提供服务的接口和实现类

AddService.java,有两个注意事项。 (1)在接口定义前添加@WebService标记,表示这是WebService服务。 否则,在生成服务端时将无法找到合适的接口。 )2)此处@WebService标记的targetNamespace请务必填写内容。 否则,在生成web服务端时将报告以下错误: 此名称空间的开头是包名称的逆序。

iwab 0014 eunexpectedexceptionoccurred.the name ' ' isnotlegalforjdom/XML namespaces 3360 namespaceurismustbenon-nullandnon-empty strings.org.JDOM.illegalnameeexception : the name ' is not XML importJavaX.jws.web服务; @ web service (target namespace=' http://service.demo.ws/' ) publicinterfaceaddservice { public int BDD ) inta,int b ); } AddServiceImpl.java,同样在定义此实现类之前,标记@WebService并指向该实现的接口。

package ws.demo.service.impl; importJavaX.jws.web服务; import ws.demo.service.addservice; @ web服务(endpoint interface=' ws.demo.service.addservice ' ) publicclassaddserviceimplementsaddservice ) @ ov

3、生成WebService服务(file-new-other-web services-web service ),选择在Service implementation中提供服务的实现类。

您可以选择在Configuration中运行的环境。 我在使用Tomcat 7.0和CXF。 选择CXF作为运行时环境时,必须首先导入CXF环境并执行Eclipse。 否则,向unabletoaddthefollwingfacetstoprojectws-server 3333报告导入步骤如下:

4、以上配置完成后,选择web服务(file-new-other-web服务-web服务)、提供服务的实现类,配置运行环境,点击Next,提供服务的接口

5、选择要标记的内容,如@WebMethod、@WebParam、@RequestWrapper、@ResponseWrapper、@WebResult等,选择完毕后单击Next

6 .选择Java2WS的配置信息。 也可以选择SOAP版本,然后单击Next。 Eclipse在后台生成相应的文件,并在控制台中打印日志。

jva2ws-CPE :workspacews-serverbuildclasses-se :workspacews-server. cxf tmp/src-d

wrapperbean ws.demo.service.impl.AddServiceImpl java2ws - Apache CXF 3.1.6 六月 14, 2016 3:51:03 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://impl.service.demo.ws/}AddServiceImplService from class ws.demo.service.AddService

生成后的项目结构:

生成服务端时要将Eclipse的jre改成jdk包中的jre,否则会报:

Exception in thread "main" java.lang.NullPointerException at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:190) at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:144) at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:139)

同时要将Tomcat会读取默认的jre,这里也需要将Tomcat的jre改掉。修改完成后的jre版本:

7、 按理说现在需要的文件都已经生成了,程序应该可以运行了,但是实际情况并非如此,先启动服务器,结果报错:

严重: Context initialization failed org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxf-extension-soap.xml] Offending resource: ServletContext resource [/WEB-INF/cxf-beans.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf-extension-soap.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)

这是因为WEB-INF目录下的cxf-beans.xml文件里有

<import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

这几段将cxf.xml、cxf-extension-soap.xml、cxf-servlet.xml引进来了,但是我使用的CXF3.1.6版本并不会生成这几个文件,所以需要将这几段删除。同时还要删除cxf-services-ws-discovery-api-3.1.4.jar、cxf-services-ws-discovery-service-3.1.4.jar、cxf-services-wsn-api-3.1.4.jar、cxf-services-wsn-core-3.1.4.jar这几个jar包,否则在启动后虽然服务可以发布,但是过一段时间就会报prefix wsdp is not bound to a namespace。
现在是否就可以运行了呢?先运行以下服务器,结果又报错:

严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addservice': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: Could not find definition for service {http://service.demo.ws/}AddServiceImplService. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1572)

这是因为在命名空间http://service.demo.ws/下找不到AddServiceImplService,观察项目结构发现根本就没有叫这个名字的类,那这个名字是从哪里来的呢?再看cxf-beans.xml,里面有一段:

<jaxws:endpoint xmlns:tns="http://service.demo.ws/" id="addservice" implementor="ws.demo.service.impl.AddServiceImpl" wsdlLocation="wsdl/addserviceimpl.wsdl" endpointName="tns:AddServiceImplPort" serviceName="tns:AddServiceImplService" address="/AddServiceImplPort"> <jaxws:features> <span style="white-space:pre"> </span><bean class="org.apache.cxf.feature.LoggingFeature" /> </jaxws:features> </jaxws:endpoint>

可以看到,这里的jaxws:endpoint属性里面有serviceName的属性,属性名为AddServiceImplService,这些都是框架自动生成文件时加上的,现在把这些不需要的删去,改为如下的内容:

<jaxws:endpoint xmlns:tns="http://service.demo.ws/" id="addservice" implementor="ws.demo.service.impl.AddServiceImpl" wsdlLocation="wsdl/addserviceimpl.wsdl"> <jaxws:features> <bean class="org.apache.cxf.feature.LoggingFeature" /> </jaxws:features> </jaxws:endpoint>

再启动服务器,启动成功,访问http://localhost:8080/ws-server/services/,出现如下结果:

再点击wsdl的链接,可以看到生成的wsdl文件:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.demo.ws/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://service.demo.ws/" name="AddServiceImplService" targetNamespace="http://impl.service.demo.ws/"> <wsdl:import location="http://localhost:8081/ws-server/services/AddServiceImplPort?wsdl=AddService.wsdl" namespace="http://service.demo.ws/"></wsdl:import> <wsdl:binding name="AddServiceImplServiceSoapBinding" type="ns1:AddService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="add"> <soap:operation soapAction="" style="document" /> <wsdl:input name="add"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="addResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="AddServiceImplService"> <wsdl:port binding="tns:AddServiceImplServiceSoapBinding" name="AddServiceImplPort"> <soap:address location="http://localhost:8081/ws-server/services/AddServiceImplPort" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

服务端编写成功。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。