加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice简单学习笔记

发布时间:2020-12-16 22:30:20 所属栏目:安全 来源:网络整理
导读:1. Web Service中的几个重要术语 1.1 WSDL:web service definition language 直译 : WebService定义语言 ? 1. 对应一种类型的文件 .wsdl 2. 定义了 web service 的服务器端与客户端应用交互传递请求和响应数据的格式和方式 3. 一个web service对应一个 唯一

1. Web Service中的几个重要术语

1.1 WSDL:web service definition language

直译 : WebService定义语言

?

1. 对应一种类型的文件.wsdl

2. 定义了web service的服务器端与客户端应用交互传递请求和响应数据的格式和方式

3. 一个web service对应一个唯一的wsdl文档

1.2 SOAP:simple object access protocal

直译: 简单对象访问协议

?

1.????? 是一种简单的、基于HTTPXML的协议,用于在WEB上交换结构化的数据

2.????? soap消息:请求消息响应消息

3.????? http+xml片断

?

1.3 SEI:WebService EndPoint Interface(终端)

直译: web service的终端接口,

?

1. 就是WebService服务器端用来处理请求的接口

2、java调用免费webservice

第一步:学会使用eclipse的webservice视图。

第二步:取得webservice的wsdl文档,即在访问路径后面加上?wsdl

第三步:得到wsdl文档后,在cmd命令中定位到项目的src目录,使用的jdk的命令“wsimport -keep wsdl的路径”生成java类。

说明: 直接生成客户端代码会抛异常,无法生成客户端代码,解决办法:

1.????? 将对应的wsdl文档保存到本地

2.????? 修改wsdl文档的部分内容:

?<s:element ref="s:schema" /><s:any />?替换成?<s:any minOccurs="2" maxOccurs="2"/>

备注: 这个是Java调用net的webservice都有的问题

?

第四步:根据wsdl文档结构调用代码


我们可以看见层级关系正好是类的调用过程。

【代码】

    public static void main(String[] args) {
       // 这些调用代码由wsdl文档结构得来
       WeatherWS ws = new WeatherWS();
       WeatherWSSoap wwsoap = ws.getWeatherWSSoap();
       List<String> aos = wwsoap.getWeather("大连",null).getString();
      
       System.out.println(aos);
    }

3、使用CXF编写客户端

??? 其实使用CXF开发client非常简单,和使用jdk是一样的区别在于要配置CXF的环境变量,之后生成的java类文件用指令:wsdl2java+ 路径,其他操作一样。

4、使用CXF编写服务端并发布

??? 发布一个简单的webservice很简单,只需要以下几个类:

【接口】

/*
 * SEI:
 */
@WebService
public interface HelloWS {
 
    @WebMethod
    public String sayHello(String name);
}


【实现类】

/*
 * SEI的实现
 */
@WebService
public class HelloWSImpl implements HelloWS {
 
    @Override
    public String sayHello(String name) {
       System.out.println("serversayHello()"+name);
       return "Hello " +name;
    }
}

【发布类】

/*
 * 发布Web Service
 */
public class ServerTest {
 
    public static void main(String[] args) {
 
       String address= "http://locahost:8989/day01_ws/hellows";
       Endpoint.publish(address,new HelloWSImpl());
       System.out.println("发布webservice成功!");
    }
}

5、CXF拦截器

??? 拦截器一般有四种,客户端有入拦截器和出拦截器,服务端有入拦截器和出拦截器。拦截器只需要在客户端请求和服务端发布的时候使用,因此关键代码如下:

【客户端】

    public static void main(String[] args) {
       HelloWSImplService factory = new HelloWSImplService();
       HelloWS hellWS = factory.getHelloWSImplPort();
      
       //发送请求的客户端对象
       Client client = ClientProxy.getClient(hellWS);
       //客户端的日志出拦截器
       List<Interceptor<? extends Message>>outInterceptors = client.getOutInterceptors();
       outInterceptors.add(new LoggingOutInterceptor());
       //客户端的日志入拦截器
       List<Interceptor<? extends Message>>inInterceptors = client.getInInterceptors();
       inInterceptors.add(new LoggingInInterceptor());
      
       String result = hellWS.sayHello("BOB");
       System.out.println("client"+result);
    }

【服务端】

    public static void main(String[] args) {
       String address = "http://192.168.10.165:8888/day01_ws/datatypews";
       Endpoint endpoint = Endpoint.publish(address,new HelloWSImpl());
       System.out.println(endpoint);
       EndpointImpl endpointImpl  = (EndpointImpl)endpoint;
      
       //服务端的日志入拦截器
       List<Interceptor<? extends Message>>inInterceptors = endpointImpl.getInInterceptors();
       inInterceptors.add(new LoggingInInterceptor());
      
       //服务端的日志出拦截器
       List<Interceptor<? extends Message>>outInterceptors = endpointImpl.getOutInterceptors();
       outInterceptors.add(new LoggingOutInterceptor());
      
       System.out.println("发布webservice成功!");
    }

6、CXF自定义拦截器

??? 由上面拦截器可以看出,只需要new一个拦截器就可以应用了,因此自定义的拦截器如同系统拦截器一样使用。

【拦截器】

public class AddUserInterceptorextends AbstractPhaseInterceptor<SoapMessage> {
 
    private String name;
    private String password;
 
    public AddUserInterceptor(String name,String password) {
       super(Phase.PRE_PROTOCOL);// 准备协议化时拦截
       this.name = name;
       this.password = password;
    }
 
    @SuppressWarnings("deprecation")
    @Override
    public void handleMessage(SoapMessage msg) throws Fault {
       List<Header> headers = msg.getHeaders();
 
       /*
        * <atguigu><name>xfzhang</name> <password>123456</password></atguigu>
        */
       Document document = DOMHelper.createDocument();
       Element rootEle = document.createElement("atguigu");
       Element nameELe = document.createElement("name");
       nameELe.setTextContent(name);
       rootEle.appendChild(nameELe);
       Element passwordELe = document.createElement("password");
       passwordELe.setTextContent(password);
       rootEle.appendChild(passwordELe);
 
       headers.add(new Header(new QName("atguigu"),rootEle));
       System.out.println("client handleMessage()....");
    }
 
}


【调用】

调用和系统拦截器一样调用。

7、跨域问题

??? 其实,用原生的ajax和jquery来请求webservice的时候是会出现跨域问题的,因此我们先请求本地的服务(如servlet),再通过HttpURLConnection发请求就不会出现跨域问题了,关键代码如下:

【关键代码】

protected void doPost(HttpServletRequestrequest,HttpServletResponse response) throws ServletException,IOException {
         Stringname = request.getParameter("name");
         System.out.println("doPost"+name);
        
         Stringdata = "<soap:Envelopexmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><ns2:sayHelloxmlns:ns2='
         URLurl = new URL("http://192.168.10.165:8888/day01_ws/datatypews");
        
         HttpURLConnectionconnection = (HttpURLConnection) url.openConnection();
        
         connection.setRequestMethod("POST");
         connection.setDoOutput(true);
         connection.setDoInput(true);
         connection.setRequestProperty("Content-Type","text/xml;charset=utf-8");
        
         OutputStreamos = connection.getOutputStream();
         os.write(data.getBytes("utf-8"));
        
         intresponseCode = connection.getResponseCode();
         if(responseCode==200){
                   InputStreamis = connection.getInputStream();//String xml
                   System.out.println("return"+is.available());
                  
                   response.setContentType("text/xml;charset=utf-8");
                   ServletOutputStreamoutputStream = response.getOutputStream();
                  
                   byte[]buffer = new byte[1024];
                   intlen = 0;
                   while((len=is.read(buffer))>0){
                            outputStream.write(buffer,len);
                   }
                   outputStream.flush();
         }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读