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

webservice学习第一章

发布时间:2020-12-16 21:57:52 所属栏目:安全 来源:网络整理
导读:什么是webservice? ** webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间交互的规范。 ** 例如:天气预报对外提供接口,供别人调用获取天气的信息。 又比如在Windows server上面开发了一个C#应用A,在linux服务器上面又用java开发

什么是webservice?

** webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间交互的规范。 **

例如:天气预报对外提供接口,供别人调用获取天气的信息。 又比如在Windows server上面开发了一个C#应用A,在linux服务器上面又用java开发了一个应用B,两个应用直接互相调用,获取对方的业务数据。

使用CXF实现客户端和服务器交互的HelloWorld程序。

客户端代码

package com.xukaiqiang.webservice.test;

import com.xukaiqiang.webservice.HelloWorld;
import com.xukaiqiang.webservice.HelloWorldService;

public class Client {
	public static void main(String[] args) {
		HelloWorldService  service=new HelloWorldService();
		HelloWorld helloWorld=service.getHelloWorldPort();
		System.out.println(helloWorld.say("_xukaiqiang"));
	}
}

服务器端代码

package com.xukaiqiang.webservice.impl;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.xukaiqiang.webservice.HelloWorld;


/**
 * @ClassName: Server
 * @Description: CXF发布webservice接口
 * @author xukaiqiang
 * @date 2016年12月16日 下午6:21:35
 * @modifier
 * @modify-date 2016年12月16日 下午6:21:35
 * @version 1.0
*/
public class Server {
	public static void main(String[] args) {
		System.out.println("web service  start.");
		HelloWorld helloWorldImpl=new HelloWorldImpl();
		String address="http://192.168.2.169/helloWorld";
		JaxWsServerFactoryBean  factoryBean =new JaxWsServerFactoryBean();
		//设置暴露地址
		factoryBean.setAddress(address);
		//接口类
		factoryBean.setServiceClass(HelloWorld.class);
		factoryBean.setServiceBean(helloWorldImpl);
		//创建webservice接口
		factoryBean.create();
		System.out.println("web service  started");
	}
}

server依赖

<dependencies>
  	<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
	<dependency>
	    <groupId>org.apache.cxf</groupId>
	    <artifactId>cxf-rt-frontend-jaxws</artifactId>
	    <version>3.1.6</version>
	</dependency>
	  	<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
	<dependency>
	    <groupId>org.apache.cxf</groupId>
	    <artifactId>cxf-core</artifactId>
	    <version>3.1.6</version>
	</dependency>
	 <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty -->
	<dependency>
	    <groupId>org.apache.cxf</groupId>
	    <artifactId>cxf-rt-transports-http-jetty</artifactId>
	    <version>3.1.6</version>
	</dependency>
  </dependencies>

接口和接口实现类

package com.xukaiqiang.webservice.impl;

import javax.jws.WebService;

import com.xukaiqiang.webservice.HelloWorld;
@WebService
public class HelloWorldImpl implements HelloWorld {
	public String say(String words) {
		return "Hello"+words;
	}

}

客户端的代码需要使用wsdl2java命令进行生成:

首先运行server端程序。 然后再要生成代码的目录下面如:client项目的

C:UsersxukaiqianggitwebserviceWS_Clientsrcmainjava文件,

在此处执行命令

wsdl2java http://192.168.2.169/helloWorld?wsdl

就会生成源码到该目录下面。

源码github下载地址

(编辑:李大同)

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

    推荐文章
      热点阅读