【笔记】WebService开发服务端的两种方式

本文介绍Webservice的概念并提供两种实现方式:一种是使用JDK直接发布;另一种是使用Apache CXF框架发布。这两种方式均包括接口定义、实现类及服务启动步骤。

简介:
webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互。
这里写图片描述

首先准备好要发布的功能服务代码,然后通过不同方式发布。

1.【接口】

package com.jul_11th.WebService;

import javax.jws.WebService;

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

2.【实现类】

package com.jul_11th.WebService;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {

    @Override
    public String say(String str) {
        return "Hello,"+str;
    }

}

一、使用JDK实现

3.【server】

package com.jul_11th.server;

import javax.xml.ws.Endpoint;

import com.jul_11th.WebService.HelloWorld;
import com.jul_11th.WebService.HelloWorldImpl;

public class Server {
    public static void main(String[] args) {
        System.out.println("web service start!");
        HelloWorld implementor = new HelloWorldImpl();
        String address = "http://192.168.38.233/helloWorld";
        Endpoint.publish(address, implementor);
        System.out.println("web service started!");
    }
}

二、使用Apache CXF框架实现

3.【server】

package com.jul_11th.server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.jul_11th.WebService.HelloWorld;
import com.jul_11th.WebService.HelloWorldImpl;

public class Server {
    public static void main(String[] args) {
        System.out.println("web service start!");
        HelloWorld implementor = new HelloWorldImpl();
        String address = "http://192.168.38.233/helloWorld";
        JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
        factoryBean.setAddress(address);//设置暴露地址
        factoryBean.setServiceClass(HelloWorld.class);//接口类
        factoryBean.setServiceBean(implementor);//设置实现类
        factoryBean.create();//创建WebService接口
        System.out.println("web service started!");
    }
}

源码下载:http://download.csdn.net/detail/jul_11th/9889062

谢谢支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值