加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF开发WebServices接口

发布时间:2020-12-16 23:32:23 所属栏目:安全 来源:网络整理
导读:WebServices调用基本采用以下技术: axis1 axis2 xfire cxf 目前应该cxf技术最新,前面的逐渐被淘汰。我们这里使用CXF来访问WebServices服务。 我们采用JDK1.6提供给我们的ws创建服务端 项目采用maven构建,pom.xml依赖如下 dependencygroupIdorg.apache.mav

WebServices调用基本采用以下技术:

axis1 axis2 xfire cxf


目前应该cxf技术最新,前面的逐渐被淘汰。我们这里使用CXF来访问WebServices服务。


我们采用JDK1.6提供给我们的ws创建服务端

项目采用maven构建,pom.xml依赖如下

	<dependency>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>2.4.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf.karaf</groupId>
			<artifactId>apache-cxf</artifactId>
			<version>2.7.11</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging-api</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>


1.创建接口,提供用注解提示编译器 这个是WebServices接口。

import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHello(String str);
}

2.完成接口的实现

import javax.jws.WebService;

@WebService(endpointInterface = "com.yonyou.wsserver.HelloWorld",serviceName = "hello")
public class WSClient implements HelloWorld {

	@Override
	public String sayHello(String str) {
		return "hello world";
	}
}

3.创建main函数启动服务端代码

import javax.xml.ws.Endpoint;

public class ServerMain {

	public static void main(String[] args) {

		HelloWorld helloWorld = new WSClient();
		Endpoint.publish("http://127.0.0.1:8080/hello",helloWorld);
		System.out.println("success");
	}

}



启动成功后访问一下 ?http://127.0.0.1:8080/hello?wsdl

接下来我们创建客户端调用服务端WebServices程序。我们使用wsdl2java,该工具在cxf的bin下

1.使用该工具 执行wsdl2java http://127.0.0.1:8080/hello?wsdl 会生成很多文件,如果有错误,直接注释或者删除就行了


2.编写代码,返回远程WebService的代理对象。执行对应接口方法就可以了

import com.yonyou.ws.server.impl.ImplMyFirstWS;

public class WSClient {
	public static void main(String[] args) {
		ImplMyFirstWS client = new ImplMyFirstWS();
		String str = client.insertData("高明");
		System.out.println(str);
	}
}
正常返回结果 ok ?我们搞定了

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读