客户端发布webservice项目,服务器端使用webservice简单例子

客户端发布webservice项目,服务器端使用webservice简单例子。

客户端

说明:运行main,生成wsdl地址

package com.wsService.third;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * 
 * @ClassName MobileService
 * @Description 手机业务类,通过webservice对外提供服务
 * 1、声明:@webservice
 * 2、发布:EndPoint
 * @author Try_go
 * @Date 2017年4月6日 下午10:22:43
 * @version 1.0.0
 */

/**
 * 注解说明:
 * @WebMethod(operationName="") 修改方法名
 * @WebResult(name="") 修改返回参数名
 * @WebParam(name="") 修改输入参数名 @WebService(serviceName="" 修改服务名
 *                    ,targetNamespace="") 修改命名空间,默认包名(取反)
 * 
 * @WebService 声明该业务类对外提供webservice服务
 * @WebMethod(exclude=true) 表示该方法不被发布
 */
@WebService(serviceName = "mobileManager", targetNamespace = "http://tttest.ws.com")
public class MobileService {
    @WebMethod(operationName = "getMobileInfo")
    public @WebResult(name = "mobile") Mobile getPhoneInfo(@WebParam(name = "osName") String osName) {
        Mobile phone = new Mobile();
        if (osName.endsWith("android")) {
            phone.setName("android");
            phone.setOwner("google");
            phone.setTotal(70);
        } else if (osName.endsWith("ios")) {
            phone.setName("ios");
            phone.setOwner("apple");
            phone.setTotal(20);
        } else if (osName.endsWith("windows phone")) {
            phone.setName("windows phone");
            phone.setOwner("microsoft");
            phone.setTotal(10);
        }
        return phone;
    }

    @WebMethod(exclude = true)
    public void hello(String hello) {
        System.out.println("你好" + hello);
    }

    public static void main(String[] args) {
        String address = "http://127.0.0.1:8080/ws/MobileService";
        /**
         * 发布webservice服务 publish中第一个为地址,第二位服务的实现对象
         */
        Endpoint.publish(address, new MobileService());
        System.out.println("wsdl地址:" + address + "?WSDL");
    }

}

实体类

package com.wsService.third;

public class Mobile {
    private String name; //操作系统名
    private String owner; //拥有者
    private int total;  //市场占有率
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
}

服务端

说明:先要使用wsimport方式生成本地代理,地址用客户端生成的wsdl地址,后将生成的代码复制到项目中。注意,在运行服务端的代码,想得到结果,客户端要保持运行状态。

package com.ws.third;

/**
 * 
 * @ClassName TestPhone
 * @Description 得到third包中发布的webservice的数据
 * @author Try_go
 * @Date 2017年4月6日 下午10:52:59
 * @version 1.0.0
 */
public class TestMobile {
    public static void main(String[] args) {

        // 得到服务
        MobileManager ws = new MobileManager();
        // 访问方式
        MobileService port = ws.getMobileServicePort();
        // 输入参数,得结果
        Mobile info = port.getMobileInfo("ios");
        System.out.println(info.getName() + info.getOwner() + info.getTotal());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值