要调用webservice,首先得有接口,用已经写好的接口地址在myEclipse的目标project中,右键->new web service client->
选择JAX-WS方式,点击“next”,看到以下界面:输入webservice接口地址,然后选择你要生成客户端的package包,不选择默认是项目的default包。
点击“next”,进入验证环节,再次点击“next”,进入以下界面:如果在新建项目时new的是web service project,这两项不需要勾选,否则要勾选上。
最后点击“finish”完成。这样就生成了webservice的客户端。
接下来,在目标project程序中调用.net的webservice接口,推荐的方式是在资源文件中配置adapterDomain(等于wsdlLocation值),namespaceURI(等于targetNamespace的值),localPart(等于service的name)三个变量,这样是为了程序的可移植性更好,例如:在default.properties文件中定义这三个变量:
adapterDomain=http://192.168.1.104/ssd/AdapterService/AdapterService.asmx?wsdlnamespaceURI=http://tempuri.org/localPart=AdapterService
需要声明的是,这三个变量可以在http://192.168.1.104/ssd/AdapterService/AdapterService.asmx?wsdl这个接口地址或者生成的客户端的注解为@WebServiceClient的类中找到。
然后,在本项目新建一个CallWebService类:
1 public class CallWebservice { 2 static Logger logger = Logger.getLogger(CallWebservice.class.getName()); 3 4 private String adapterDomain; 5 private String namespaceURI; 6 private String localPart; 7 8 public CallWebservice(String adapterDomain,String namespaceURI,String localPart) { 9 super();10 this.adapterDomain = adapterDomain;11 this.namespaceURI = namespaceURI;12 this.localPart = localPart;13 }14 public AdapterServicePortBinding initPortBind() throws Exception{15 AdapterServicePortBinding adapterService = null;16 URL url;17 url = new URL(adapterDomain);18 QName qName = new QName(namespaceURI,localPart);19 javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qName);20 adapterService = service.getPort(AdapterServicePortBinding.class);21 return adapterService;22 }23 24 25 public String getAll() throws Exception {26 return initPortBind().getAll();27 }
AdapterServicePortBinding 是暴露了接口方法的类,不同的写法可能有不同的结果。这里方法getAll()是在接口中声明的方法,且在AdapterServicePortBinding 类中发布到网络中的。
这时,我们就可以在程序中调用这个接口的方法了。
但在这之前,要先写一个读取资源文件的util工具类:PropertiesUtil
1 public class PropertiesUtil { 2 3 static Logger logger = Logger.getLogger(PropertiesUtil.class.getName()); 4 private static PropertiesUtil instance = null; 5 private static String file = null; 6 private Properties props = new Properties(); 7 private static InputStream in = null; 8 9 public static synchronized PropertiesUtil getInstance() {10 if (instance == null) {11 instance = new PropertiesUtil();12 }13 return instance;14 }15 16 public PropertiesUtil() {17 }18 19 public void init(String inputFile) {20 if (!inputFile.equals(file)) {21 file = inputFile;22 try {23 // in = new BufferedInputStream(new FileInputStream(inputFile));24 in = PropertiesUtil.class.getClassLoader().getResourceAsStream(inputFile);25 props.load(in);26 } catch (Exception e) {27 logger.error("错误:配置文件操作失败, 初始化属性文件失败");28 logger.error(e);29 System.out.println("错误:配置文件操作失败, 初始化属性文件失败\n" + DateUtil.getInstance().formatterDate(new Date()) + " " + getClass().getName());30 //e.printStackTrace();31 }32 }33 }34 35 public String readValue(String key) {36 String value = props.getProperty(key);37 return value;38 }
好了,现在可以来调用了。
例如,我在UserService类中这样写:
1 public String findAuthorTreeStr(){ 2 //调用方法获取CallWebservice 对象,并调用接口方法 3 List list = this.getService().getAll(); 4 5 } 6 7 /** 8 * 呼叫Webservice,获取实例 9 * @return10 */11 public CallWebservice getService(){12 PropertiesUtil propertiesUtil = PropertiesUtil.getInstance();13 propertiesUtil.init("default.properties");14 adapterDomain = propertiesUtil.readValue(adapterDomain);15 namespaceURI = propertiesUtil.readValue(namespaceURI);16 localPart = propertiesUtil.readValue(localPart);17 18 CallWebservice service = new CallWebservice(adapterDomain, namespaceURI, localPart);19 return service;20 }
这样就完成了一次webservice调用。笔记结束。