CXF框架实现WebService
发布时间:2020-12-16 21:43:35 所属栏目:安全 来源:网络整理
导读:Apache CXF介绍:http://cxf.apache.org/docs/index.html 一、使用CXF开发Web Service服务端: 每个Web Service组件需要2个部分:接口和实现类: 1、新建maven工程cxfWebService 工程目录结构: 这是pom.xml文件的内容 project xmlns="http://maven.apache.o
Apache CXF介绍:http://cxf.apache.org/docs/index.html一、使用CXF开发Web Service服务端:
每个Web Service组件需要2个部分:接口和实现类:
1、新建maven工程cxfWebService
工程目录结构:
这是pom.xml文件的内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.webService</groupId> <artifactId>cxfWebService</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cxfWebService</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <cxf.version>2.2.3</cxf.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- Jetty is needed if you're are not using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> </dependencies> </project>? ? 2、开发一个webservice业务接口,方法使用@WebService修饰。 @WebService public interface HelloWorld { String sayHi(String name); } package com.weir; import java.util.Date; import javax.jws.WebService; @WebService(endpointInterface="com.weir.HelloWorld",serviceName="HelloWorldWs")//指定webservice所实现的接口以及服务名称 public class HelloWorldImpl implements HelloWorld{ public String sayHi(String name) { // TODO Auto-generated method stub return name+"您好!现在时间是:"+new Date(); } }4、暴露Web Service的函数,运行函数暴露Web Service: package com.weir; import javax.xml.ws.Endpoint; public class ServiceMain { public static void main(String args[]){ HelloWorld hw = new HelloWorldImpl(); // 调用Endpoint的publish方法发布Web Service String address="http://localhost:8080/helloWorldService"; Endpoint.publish(address,hw); System.out.println("Web Service暴露成功!"); } }打开浏览器输入http://localhost:8080/helloWorldService?wsdl,暴露服务成功的情况下可以看到wsdl描述文件 二、开发Web Service客户端:
1、新建java project?cxfWebServiceClient
选择刚才的wsdl地址
选择生成的代码位置,我们这边选择新建的工程cxfWebServiceClient
可以看到,我们的工程底下多了这几个文件
3、在客户端写测试代码
package com.weir.test; import java.rmi.RemoteException; import com.weir.HelloWorldProxy; public class TestService { public static void main(String args[]){ HelloWorldProxy helloWorldProxy = new HelloWorldProxy(); try { String s = helloWorldProxy.sayHi("weir"); System.out.println("调用webservice:"+s); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }运行main函数,成功调用WebService (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- docker – 使用“tail -f / dev / null”来保持容器意外失
- angularjs – 如何从外部Angular访问/更新$rootScope
- 异步刷新Browser的两种方法
- angular2 routes TypeError:无法读取属性’forRoot’
- scala – 如何更改SBT命令中的设置?
- 基于Bootstrap3制作响应式布局网站(一)
- twitter-bootstrap – 什么是Vanilla Bootstrap?
- file – vim:gf的反义词是什么?
- angular – 使用ngModel访问输入文件[]
- bootstrap+jquery+mustache+springmvc+idworker整合demo代码