您的位置:首页 > 其它

发布WebService 1.1

2014-06-23 21:16 232 查看
webservice1.1是基于jdk发布的

package cn.itcast.service01;

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

@WebService
public class HelloService {

public String sayHello(String name)
{
System.out.println("say hello called");
return "hello " +name;
}
//main方法不能发布的, 发布的方法不能是静态 的
public static void main(String[] args)
{
//启动一个新线程   地址  内容
Endpoint.publish("http://192.168.151.42:5678/hello", new HelloService());
System.out.println("herer ");
}
}


测试:

一、用户访问http://192.168.151.42:5678/hello?wsdl 然后

cmd下输入 wsimport -s . http://192.168.151.42:5678/hello?wsdl
然后把代码下下来 去掉其中的*.class

复制代码到项目测试

在http://192.168.151.42:5678/hello?wsdl中采用从底向上的模式看

package cn.itcast.testService;

import cn.itcast.service01.HelloService;
import cn.itcast.service01.HelloServiceService;

public class AppTest {

public static void main(String[] args)
{
/**
*  wsdl 地址 service name="HelloServiceService"
*/
HelloServiceService  service=new HelloServiceService();

HelloService soap=service.getHelloServicePort();
String str=soap.sayHello("this is sss ");
System.out.println(str);
}
}


这样 就能实现 在客户端访问 服务端的WebService

还有一种客户端写法实现访问服务端, 这种方法其实和第一种本质是一样的,第一种方法采用继承接口的形式,客户端拷贝多个文件。

第二种方法客户端单单拷贝接口,因为继承这个接口的构造方法是protected,所以可以用第二种方法,代理对象

二 利用生成的接口 和jdk提供的 Service 等类 实现访问 这种方法比较折中,拷贝一个文件又能用

服务器端添加方法

public String sayHello2(String name,int n)
{
System.out.println("say hello called       --------------------"+n);
return "hello " +name;
}


然后 wsimport -s -p cn.itcast.hello http://192.168.151.42:5678/hello?wsdl
然后单单拷贝生成的接口 HelloService

然后以下代码

package cn.itcast.testService;

import java.net.MalformedURLException;
import java.net.URL;

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

import cn.itcast.hello.HelloService;

public class AppTest2 {

public static void main(String[] args) throws MalformedURLException
{                                                                                //命名空间                                                              service
Service s = Service.create(new URL("http://192.168.151.42:5678/hello?wsdl"), new QName("http://service01.itcast.cn/", "HelloServiceService"));
//命名空间                            服务绑定的port
HelloService hs = s.getPort(new QName("http://service01.itcast.cn/","HelloServicePort"), HelloService.class);

String str = hs.sayHello2("lisi",10);
System.out.println(str);
System.out.println(hs.getClass().getSimpleName());
}
}


就可以访问了 服务端了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: