webservice 从入门到精通(二)helloworld(Webservice + cxf)

1  官网下jar包,搭工程(略)

http://cxf.apache.org/

 

2 代码 

1
2
3
4
5
6
7
8
9
10
package com.mangoubiubiu.cxf.test;
 
import javax.jws.WebService;
 
@WebService //对外发布服务
public interface HelloWorld {
 
    public String sayHello(String name);
     
}

 

1
2
3
4
5
6
7
8
9
10
11
12
package com.mangoubiubiu.cxf.test;
 
import javax.jws.WebService;
 
public class HelloWorldImpl implements HelloWorld {
 
    @Override
    public String sayHello(String name) {
        return "cxf1022"+name;
    }
 
}

3 服务端发布(服务端放实现类

复制代码
package com.mangoubiubiu.cxf.test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsClientFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {
    
    public static void main(String args[]){
        
        JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
        
        jaxWsServerFactoryBean.setAddress("http://127.0.0.1:9999/cxf1022_server");
        
        jaxWsServerFactoryBean.setServiceClass(HelloWorldImpl.class);
        Server server=    jaxWsServerFactoryBean.create();
        server.start();
    }

}
复制代码

4 访问WSDL

http://127.0.0.1:9999/cxf1022_server?wsdl

 

 

5 客户端调用(客户端放接口

复制代码
package com.mangoubiubiu.cxf.test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class ClientServer {

    public static void main(String args[]){
        
    
        
        // TODO Auto-generated method stub
        //创建调用服务的类...
        JaxWsProxyFactoryBean bean=new JaxWsProxyFactoryBean();
        //设置访问地址
        bean.setAddress("http://127.0.0.1:8888/cxf1022_server");
        //设置接口类型...
        bean.setServiceClass(HelloWorld.class);
        HelloWorld us=(HelloWorld) bean.create();
        
        String data=us.sayHello("小明");
        
        System.out.println(data);
    }
    
}
复制代码

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/14833520.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(131)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起