您的位置:首页 > 其它

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

2017-04-09 22:07 666 查看
客户端发布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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: