WebService -- Java 实现之 CXF (WebService 服务器端接口)

本文介绍如何使用Apache CXF快速创建并部署一个简单的WebService应用。包括使用Maven创建项目、引入必要的依赖、编写服务接口及其实现类、启动服务等步骤。

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

1. 使用Maven创建一个quickstart项目

2. 引入依赖的Jar包

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.1.5</version>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.5</version>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>3.1.5</version>
</dependency>

3. 编写Service 接口 + 实现类

package com.example.tuo.webservice;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

  public String sayHello(String sb);

}

package com.example.tuo.webservice.impl;

import javax.jws.WebService;

import com.example.tuo.webservice.HelloWorld;

@WebService
public class HelloWorldImpl implements HelloWorld{

	public String sayHello(String sb) {
		// TODO Auto-generated method stub
		return "Hello world," +sb;
	}

}

4. 启动服务

package com.example.tuo.server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.example.tuo.webservice.HelloWorld;
import com.example.tuo.webservice.impl.HelloWorldImpl;

public class Server {

	public static void main(String[] args){
		
		System.out.println("web service starting...");
		
		
		JaxWsServerFactoryBean wsSvrFactoryBean = new JaxWsServerFactoryBean();
		String address = "http://127.0.0.1/helloWorld";
		wsSvrFactoryBean.setAddress(address);
		wsSvrFactoryBean.setServiceClass(HelloWorld.class);
		HelloWorld implementor = new HelloWorldImpl();
		wsSvrFactoryBean.setServiceBean(implementor);
		wsSvrFactoryBean.create();
		
		
		System.out.println("web service started...");
	}
}

5. 访问服务

 

 

至此,我们的第一个基于CXF的webservice已经编写完成并发布出来了。

转载于:https://www.cnblogs.com/atuotuo/p/6227830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值