WebService WSDL

服务器开发步骤:

//1.必须有这个注解
@WebService
public class HelloService {
	//2.必须要有一个公共方法
	public void doublekill() {
		System.out.println("doublekill");
	}
	
	public User getUser(int id) {
		User user=new User();
		user.setId(id);
		user.setName("张三");
		user.setPwd("123456");
		return user;
	}
}
public class PublishServer {
	public static void main(String[] args) {
		//3.发布服务对外访问地址
		Endpoint.publish("http://192.168.0.29:1669/helloService", new HelloService());
	}
}

 客户端开发步骤:
1. 根据wsdl生成本地java文件

wsimport -d . http://192.168.0.29:1669/helloService?wsdl  生成本地的class 文件

wsimport -s . +服务说明书(wsdl)的地址                       生成本地的class 文件与java文件

wsimport -s . -p(包名)+服务说明书(wsdl)的地址              生成本地的class 文件与java文件

2.粘贴生成的包到程序当中

3.调用

public class InvokeService {
	public static void main(String[] args) {
		HelloServiceService service=new HelloServiceService();
		HelloService port = service.getHelloServicePort();
		port.doublekill();
		User user = port.getUser(2);
		System.out.println(user.getName());
	}

服务端的一些注解应用

@WebService
(
		targetNamespace="www.baidu.com",	
		serviceName="HelloServicePortType",
		portName="ServicePortType"
)
public class HelloService {
	
	@WebMethod(exclude=true)
	public void doubleKill(){
		
		System.out.println("aaaa");
	}
	public
	@WebResult(name="date")
	String getDate(
			@WebParam(name="date")
			String date){
		
		
		
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return dateFormat.format(new Date());
	}
	
	@WebMethod(operationName="getUserByName")
	public User getUserById(int id){
		User user=new User();
		user.setId(id);
		user.setMomo("123456664");
		user.setUsername("老冯");
		return user;
	}
}

猜你喜欢

转载自h496950806.iteye.com/blog/2282953