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

webservice(二)

发布时间:2020-12-16 21:54:16 所属栏目:安全 来源:网络整理
导读:上次写到我们生成xml,我们想到xml要是可以生成接口不就是可以实现了吗,那我们试一下! 如图所示,wsimport -s +保存地址 -p 包名 +wsdl文档url 生成了一些java类 这时我们可以直接用MyServiceImpl生成接口 webClient.java package test; import javax.xml.

上次写到我们生成xml,我们想到xml要是可以生成接口不就是可以实现了吗,那我们试一下!

java 生成接口


如图所示,wsimport -s +保存地址 -p 包名 +wsdl文档url
生成了一些java类

这里写图片描述


这时我们可以直接用MyServiceImpl生成接口
webClient.java

package test;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;

/** * Created by caicai on 2016/12/7. */
public class WebClient {
    public static void main(String args[]) {
        try {
            URL url = new URL("http://localhost:8990/test?wsdl");
            QName qName = new QName("http://test/","MyServiceImplService");
            Service service=Service.create(url,qName);
            MyService myService=service.getPort(MyService.class);
            System.out.println("加法:"+myService.add(4,3));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }
}

webTest.java

package test;


import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.message.Message;
import test.interceptor.AddUserInterceptor;
import test.interceptor.CheckUserInterceptor;

import javax.xml.ws.Endpoint;
import java.util.List;

/** * Created by caicai on 2016/12/7. */
public class WebTest {
    public static void main(String args[]){
        String address="http://localhost:8990/test";
        EndpointImpl endpoint = (EndpointImpl) Endpoint.publish(address,new MyServiceImpl());
        //服务端入日志拦截器
        List<Interceptor<? extends Message>> inInterceptors = endpoint.getInInterceptors();
        inInterceptors.add(new CheckUserInterceptor());
        //服务器出日志拦截器
       /* List<Interceptor<? extends Message>> outInterceptors = endpoint.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor());*/
    }
}

这时我们就完成了对webservice的初步认识,同时实践了一下,自后我们 写一个调用webservice的小项目,天气预报!期待中……….

(编辑:李大同)

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

    推荐文章
      热点阅读