使用cxf发布webservice

本文详细介绍如何使用CXF发布WebService,包括不同JDK版本下的CXF版本选择、Maven依赖配置、Java接口及实现类定义、服务发布过程以及客户端代码生成与测试。通过实例演示了整个发布流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cxf版本和jdk版本

jdk版本CXF版本
java 62.2.10
java 72.2.x
java 83.x
java 9+3.3.x

maven依赖

		<properties>
			<maven.compiler.source>1.8</maven.compiler.source>
			<maven.compiler.target>1.8</maven.compiler.target>
			<cxf.version>2.7.18</cxf.version>
		</properties>
		
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>${cxf.version}</version>
		</dependency>

java接口和实现类

package com.sky.cxf01.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface HelloService {
	@WebMethod
	@WebResult
	String SayHello(@WebParam String msg);
}
package com.sky.cxf01.service.impl;
import com.sky.cxf01.service.HelloService;
public class HelloServiceImpl implements HelloService {
	@Override
	public String SayHello(String msg) {
		return "Hello, " + msg;
	}
}

发布服务

package com.sky.cxf01.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.sky.cxf01.service.impl.HelloServiceImpl;
public class Server {
	public static void main(String[] args) throws InterruptedException {
		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
		factory.setServiceClass(HelloServiceImpl.class);
		factory.setAddress("http://localhost:9000/ws/HelloWorld");
		factory.create();
		System.out.println("start...");
		Thread.sleep(60 * 1000);
		System.out.println("Server exit...");
		System.exit(-1);
	}
}

生成客户端代码测试

https://blog.youkuaiyun.com/qq_26264237/article/details/97040977

客户类测试

package com.sky.cxf01.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.sky.cxf01.service.HelloService;
public class Client {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(HelloService.class);
		factory.setAddress("http://localhost:9000/ws/HelloWorld");
		HelloService service = (HelloService) factory.create();
		System.err.println(service.SayHello("GG"));
	}
}

启动服务后访问:http://localhost:9000/ws/HelloWorld?wsdl
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26264237

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值