Apache CXF实现WebService发布和调用

第一种方法:不用导入cxf jars

服务端:

1、 新建Web工程

2、新建接口和实现类、测试类

 目录结构图如下:

接口代码:

复制代码
package com.cxf.spring.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface IGreetingService {

    @WebMethod
    public String greeting(String name);
    
    
}
复制代码

实现类代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.cxf.spring.service;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
@WebService
public class GreetingServiceImpl implements IGreetingService{
 
    @WebMethod
    @Override
    public String greeting(@WebParam(name ="name") String name) {
 
        return "HI:....."+name;
    }
 
}

测试类代码:

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

import javax.xml.ws.Endpoint;

import com.cxf.spring.service.GreetingServiceImpl;

public class TestSpring {

    
    public static void main(String[] args) {
        pushWS_1();
    }
    
    
    public static void pushWS_1() {
        
        String address = "http://localhost:8100/greeting";
        
        System.out.println("Server start....");
        
        Endpoint.publish(address, new GreetingServiceImpl());
        
        System.out.println("Server ready....");
        
        try {
            Thread.sleep(3*60*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Server exit");
        System.exit(0);
    }
}
复制代码

运行测试类:访问:http://localhost:8100/greeting?wsdl

客户端:

 1、新建java工程  ,配置CXF环境变量 (下载Apache CXF2.7 )

 2、CMD打开命令窗口,运行以下命令,生产客户端代码: wsdl2java.bat  -p com.cxf.spting  -client -encoding utf-8 -noAddressBinding  http://localhost:8100/greeting?wsdl  

拷贝到新建java工程的src文件下

运行GreetingServiceImpl_GreetingServiceImplPort_Client.java访问webservice

 

第二种:

新建web工程 引入cxf依赖包(最小jar)

修改以上测试类代码

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

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.cxf.spring.service.GreetingServiceImpl;
import com.cxf.spring.service.IGreetingService;

public class TestSpring {

    
    public static void main(String[] args) {
        pushWS_2();
    }
    
    public static void pushWS_2() {
    
        JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
        bean.setAddress("http://localhost:8100/greeting");
        bean.setServiceClass(IGreetingService.class);
        bean.setServiceBean(new GreetingServiceImpl());
        bean.create();
        
        System.out.println("Server ready....");
        
        try {
            Thread.sleep(3*60*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Server exit");
        System.exit(0);
    }
复制代码

运行访问

客户端:按照第一种方法执行。。。

 

另外两种调用webservice的方法

新建工程 ------测试类   -----  接口:

复制代码
package com.cxf.test;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.cxf.test.client.IGreetingService;

public class TestClient_2 {

    
    public static void main(String[] args) {  
           
        pull_1();
        
        pull_2();
    }
    
        
    public static void pull_1(){
         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
            Client client = dcf.createClient("http://localhost:8100/greeting?wsdl");  
            try {  
                Object[] objs = client.invoke("greeting", "张三");  
                System.out.println(objs[0].toString());  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
    }
    
    public static void pull_2() {
        
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    
            factory.setServiceClass(IGreetingService.class);    
            factory.setAddress("http://localhost:8100/greeting?wsdl");    
            IGreetingService service = (IGreetingService) factory.create();    
            System.out.println(">>>>>>>>Client: " + service.greeting("战士"));
        
    }    
    
}
复制代码
复制代码
package com.cxf.test.client;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(targetNamespace="http://service.spring.cxf.com/", name = "IGreetingService")
public interface IGreetingService {

    @WebMethod
    public String greeting(String name);
    
    
}
复制代码

 

posted @   凉城  阅读(1409)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示