zoukankan      html  css  js  c++  java
  • 发布WebService 1.1

     webservice1.1是基于jdk发布的

     1 package cn.itcast.service01;
     2 
     3 import javax.jws.WebService;
     4 import javax.xml.ws.Endpoint;
     5 
     6 @WebService
     7 public class HelloService {
     8     
     9     public String sayHello(String name)
    10     {
    11         System.out.println("say hello called");
    12         return "hello " +name;
    13      }
    14     //main方法不能发布的, 发布的方法不能是静态 的
    15     public static void main(String[] args)
    16     {
    17         //启动一个新线程   地址  内容 
    18         Endpoint.publish("http://192.168.151.42:5678/hello", new HelloService());
    19         System.out.println("herer ");
    20     }
    21 }

    测试:

    一、用户访问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中采用从底向上的模式看

     1 package cn.itcast.testService;
     2 
     3 import cn.itcast.service01.HelloService;
     4 import cn.itcast.service01.HelloServiceService;
     5 
     6 public class AppTest {
     7 
     8     public static void main(String[] args)
     9     {
    10         /**
    11          *  wsdl 地址 service name="HelloServiceService" 
    12          */
    13         HelloServiceService  service=new HelloServiceService();
    14 
    15         HelloService soap=service.getHelloServicePort();
    16         String str=soap.sayHello("this is sss ");
    17         System.out.println(str);
    18     }
    19 }

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

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

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

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

    服务器端添加方法

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

    然后 wsimport -s -p cn.itcast.hello  http://192.168.151.42:5678/hello?wsdl

    然后单单拷贝生成的接口 HelloService

    然后以下代码

     1 package cn.itcast.testService;
     2 
     3 import java.net.MalformedURLException;
     4 import java.net.URL;
     5 
     6 import javax.xml.namespace.QName;
     7 import javax.xml.ws.Service;
     8 
     9 import cn.itcast.hello.HelloService;
    10 
    11 
    12 public class AppTest2 {
    13 
    14     public static void main(String[] args) throws MalformedURLException
    15     {                                                                                //命名空间                                                              service
    16         Service s = Service.create(new URL("http://192.168.151.42:5678/hello?wsdl"), new QName("http://service01.itcast.cn/", "HelloServiceService"));
    17                                         //命名空间                            服务绑定的port
    18         HelloService hs = s.getPort(new QName("http://service01.itcast.cn/","HelloServicePort"), HelloService.class);
    19                             
    20         String str = hs.sayHello2("lisi",10);
    21         System.out.println(str);
    22         System.out.println(hs.getClass().getSimpleName());
    23     }
    24 }

    就可以访问了 服务端了

  • 相关阅读:
    sgg_3_hibernate详解
    sgg_2_hibernate开发步骤
    sgg_1_hibernate概述
    cookie
    exchange 普通用户可以创建通讯组
    DC 维护常用工具命令DCdiag
    cp 复制不覆盖
    powershell 结果输出显示为……
    获得用户完整的autodiscover配置文件
    【转】outlook 2016 配置自动发现
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3804962.html
Copyright © 2011-2022 走看看