您的位置:首页 > 编程语言 > Java开发

Java使用CXF编写的soap webservice接口

2018-02-11 15:21 609 查看
SOAP Webservice基于XML协议,是常用的web服务调用方式。本篇简单记叙使用第三方库CXF发布webservice的过程。
注意,Java发布webservice有多种方法,其中包括使用CXF、axis2等第三方库,和Java自带的jaxws工具。这里使用的是Apache 下的开源项目 CXF第三方库发布Java webservice。
1.下载CXF依赖包(或者使用maven,本篇不使用maven),导入到项目中。
新建Java接口,并且添加一个带参数的方法:package com.lavendor.service;

import javax.jws.WebService;

/**
* cxf接口
* @author admin
*
*/
@WebService
public interface ICXFService {

public String sayHi(String name);
}
注意:@WebService 的注解是必须的,这表示这是一个webservice接口
2.实现上面的接口和对应的方法:package com.lavendor.service;

import javax.jws.WebService;

/**
* 使用CXF框架发布webservice服务
* @author admin
*
*/

@WebService
public class CXFWebSerivce implements ICXFService{

@Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return "Hello,Welcome " + name;
}
}
3.使用CXF发布webservice:package com.lavendor.service;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
* 发布service的类
* @author admin
*
*/
public class Main {

public static void main(String[] args) {

JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(CXFWebSerivce.class);
factory.setAddress("http://localhost:8081/helloworld"
4000
);

Server server = factory.create();
server.start();
}

}
4.运行项目,在打印台看到如下信息,则表明发布成功:二月 11, 2018 2:55:36 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.lavendor.com/}CXFWebSerivceService from class com.lavendor.service.ICXFService
二月 11, 2018 2:55:37 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8081/helloworld 二月 11, 2018 2:55:38 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
二月 11, 2018 2:55:38 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
二月 11, 2018 2:55:38 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
5.打开浏览器,输入如下网址:http://localhost:8081/helloworld?wsdl



展示出如上所示的XML报文,则表示webservice发布成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: