服务端
package com.xh.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
- @WebService :将指定的类发布为webservice
- Endpoint :此类为端点服务类,它的方法publish用于将一个
- 已经添加了@WebService注解的对象绑定到一个地址的端口上。
*/
- 已经添加了@WebService注解的对象绑定到一个地址的端口上。
@WebService
public class Hello {
/** 供客户端调用方法 该方法是非静态的,会被发布
* @param name 传入参数
* @return String 返回结果
* */
public String getValue(String name){
return "欢迎您!"+name;
}
/**
* 方法上加@WebMentod(exclude=true)后,此方法不被发布;
* @param name
* @return
*/
@WebMethod(exclude=true)
public String setHello(String name){
return "您好!"+name;
}
/** 静态方法不会被发布
* @param name
* @return
*/
public static String getString(String name){
return "再见!"+name;
}
}
package com.xh.ws;
import javax.xml.ws.Endpoint;
/**
-
@author liuys
*Endpoint :此类为端点服务类,它的方法publish用于将一个 -
已经添加了@WebService注解的对象绑定到一个地址的端口上。
*/
public class ServerMain {public static void main(String[] args) {
WebServerImpl wsi = new WebServerImpl();
/**
*
* Endpoint.publish(param1 param2)
* param1:服务的发布地址 (注意端口不能被其他服务占用)
* param2:服务的实现者(要发布的类)
*
*/
Endpoint.publish(“http://localhost:8082/HelloService/services”, wsi);
System.out.println(“服务发布成功”);
}
}
运行后
cmd
输入wsimportwsimport -s D:\workspace\jax-ws-client\src -p com.sherlon.xfire -keep http://localhost:8082/HelloService/services?wsdl
package com.xh.test;
import com.xh.wsc.Hello;
import com.xh.wsc.HelloService;
public class MyClient {
public static void main(String[] args) {
// 通过实例引用获得实例工厂
HelloService hello = new HelloService();
// 通过工厂得到实现类
Hello hello2 = hello.getHelloPort();
// 通过实现类调用方法
String value = hello2.getValue(“xinhua”);
System.out.println(value);
}
}