`

WebService Demo

阅读更多

说明:java 自带的webservice,需要jdk1.6.0以上版本

服务端:

package com.wdh.ws;

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

@WebService
public class Hello {
 
 public String sayHello(String name){
  return "hello:" + name;
 }
 
 public String sayHi(String name){
  return "hi:" + name;
 }
 
 @WebMethod(exclude=true)
 public String test(){
  return "test";
 }
 
 /**
  * wsimport -d e:/  -p com.wdh.ws.client  wsdlURL
  * */
 public static void main(String[] args) {
  Endpoint.publish("http://192.168.1.116:8964/hello", new Hello());
 }
}

 

客户端:

package com.wdh.test;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.junit.Test;

import com.wdh.ws.client.Hello;
import com.wdh.ws.client.HelloService;

public class WSTest {
 
 /**
  * 需要wsimport导出的所有类
  * */
 public  void test() {
  HelloService helloService = new HelloService();
  Hello hello = helloService.getHelloPort();
  String helloStr = hello.sayHello("jack");
  String hiStr = hello.sayHi("rose");
  
  System.out.println(helloStr);
  System.out.println(hiStr);
 }
 
 /**
  * 只需要接口(Hello)
  * */
 @Test
 public void test2()throws Exception{
  URL wsdlURL = new URL("http://192.168.1.116:8964/hello?wsdl");
  /**QName(nameSpace,serviceName)*/
  Service service = Service.create(wsdlURL, new QName("http://ws.wdh.com/","HelloService"));
  Hello hello = service.getPort(Hello.class);
  String retStr = hello.sayHello(" jack");
  System.out.println(retStr);
 }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics