2.Web Service 服务端两种实现

第一种是使用jdk实现服务器接口

  这里我用maven,首先创建一个maven项目。勾选Create a simple project 选择最快构建项目

填写项目的GroupId 和ArtifactId

点击finish就完成项目了 然后选中JRE System LIbrary

右击选中Build path在选择configure build path会得到如下

然后Remove现在的jdk点击Add Library

点击Next

     我使用的是jdk1.8,不能使用jdk1.6以下的版本,因为缺少东西。使用jdk自带的实现服务器只需了解即可,一般在实际开发都是使用cxf就是我们下面所说的第二种。

创建一个包com.oyyp.webserice然后在包下面创建一个接口HelloWorld

package com.oyyp.webservice;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
	public String say(String str);
}


在创建一个包com.oyyp.webserice.impl然后在包下面创建一个实现类HelloWorldImpl

package com.oyyp.webservice.impl;  
  
import com.oyyp.webservice.HelloWorld;  
import javax.jws.WebService;  
  
@WebService  
public class HelloWorldImpl implements HelloWorld{  
  
public String say(String str) {  
    // TODO Auto-generated method stub  
    return "Hello:"+str;  
    }  
}


在com.oyyp.webserice.impl然后在包下面创建一个类Server

package com.oyyp.webservice.impl;  
  
import javax.xml.ws.Endpoint;  
  
import com.oyyp.webservice.HelloWorld;  
  
public class Server {  
  
    public static void main(String[] args) {  
        //我是按照官方文档来写的,web service start是这样写的  
        System.out.println("web service start");  
        HelloWorld implementor=new HelloWorldImpl();  
        //这里的ip是你电脑的IP地址  
        String  address="http://192.168.43.11:90/helloWorld";  
        //jdk实现暴露webservice接口  
        Endpoint.publish(address, implementor);  
        System.out.println("web service started");  
    }


得到如下图就表示你完成了jdk的实现了


我们在浏览器里访问:http://192.168.43.11:90/helloWorld/helloWorld?wsdl


说明已经成功调用了webservice接口;
这里的wsdl 是 Web Services Description Language的缩写,是一个用来描述Web服务和说明如何与Web服务通信的XML语言。WSDL是Web Service的描述语言,
用于描述Web Service的服务,接口绑定等,为用户提供详细的接口说明书。
       请求后得到的是一个xml规范文档。是一套规范,后面会具体介绍,任何语言平台技术都可以解析。


第二种是使用CXF实现实现服务器接口

第一种了解即可,我们要掌握第二种,因为CXF作为java领域主流的WebService实现框架,Java程序员有必要掌握它。

CXF主页:http://cxf.apache.org/

我们先在pom.xml中加入:

 <dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-frontend-jaxws</artifactId>
		<version>3.1.6</version>
	</dependency>
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-core</artifactId>
		<version>3.1.6</version>
	</dependency>

这里要额外加入jetty,作为webservice发布的服务器。jetty是一个内嵌的web服务器;

<dependency>  
    <groupId>org.apache.cxf</groupId>  
    <artifactId>cxf-rt-transports-http-jetty</artifactId>  
    <version>3.1.6</version>  
    </dependency>  



 我们把Server改下。换成CXF实现:

package com.oyyp.webservice.impl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.oyyp.webservice.HelloWorld;
public class Server {

	public static void main(String[] args) {
		//我是按照官方文档来写的,web service start是这样写的
		System.out.println("web service start");
		HelloWorld implementor=new HelloWorldImpl();
		//这里的ip是你电脑的IP地址
		String  address="http://192.168.43.11:90/helloWorld";
		JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
		factoryBean.setAddress(address);//设置暴露地址
		factoryBean.setServiceClass(HelloWorld.class);//接口类
		factoryBean.setServiceBean(implementor);//设置实现类
		factoryBean.create();//创建文本service接口
		System.out.println("web service started");
	}
}

运行效果和刚才一样,就是多了几行日志,是cxf的信息日志到后面我们会接触cxf日志;

提醒:可能有时候会出现端口被占用,只要更换端口号就行了。



































评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值