javaEE WebService,根据wsdl生成Java代码(wsimport命令)

wsimport命令是jdk自带的,可以根据wsdl文档生成Java客户端代码。

cmd---wsimport -s c:/aa http://192.168.0.1/hello?wsdl    (可以通过-p cn.xxx.client来指定生成的java代码的包)

HelloService.java(服务端):

package com.xxx.webservice;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
//加入WebService注解
public class HelloService {
	public String sayHello(String name){
		System.out.println("服务端的sayHello方法被调用了。。。。");
		return "hello" + name;
	}
	
	public static void main(String[] args) {
		String address = "http://192.168.0.1:8080/hello";
		Object implementor = new HelloService();
		Endpoint.publish(address, implementor);  //通过Endpoint发布到指定地址/端口

		//http://192.168.0.1:8080/hello?wsdl  查看服务的说明文档
	}
}

Client.java(客户端):

package com.xxx.client;

/**
 * 1、使用wsimport命令解析wsdl文件生成本地代码
 * 2、通过本地代码创建一个代理对象
 * 3、通过代理对象实现远程调用
 *
 */
public class Client {
	public static void main(String[] args) {
		HelloServiceService ss = new HelloServiceService();
		//创建客户端代理对象,用于远程调用
		HelloService proxy = ss.getHelloServicePort();
		String result = proxy.sayHello("小明");  //调用远程方法
		System.out.println(result);
	}
}

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82664740
今日推荐