SpringBoot集成webService
我这里用的cxf框架来搭建webService服务
我的SpringBoot版本为2.7.5
查看项目springBoot版本的方法:
1.
- 写测试类,打印springBoot版本
@Test void version(){ System.out.println(SpringBootVersion.getVersion()); }
服务端发布服务
1 导入cxf依赖
<!-- cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.5.5</version>
</dependency>
我直接选的3.5.5版本,最后可以成功运行。
2 编写接口类
package com.example.service;
import javax.jws.WebService;
/**
* @author A
* @date 2023-01-31 11:39:31
* @description
*/
@WebService(name = "myService",//向外暴露的服务名称
endpointInterface = "com.example.service.MyWebService",//服务接口全路径,指定做SEI(Service EndPoint Interface)服务端点接口
targetNamespace = "http://service.example.com"//命名空间,一般是接口的包名倒序
)
public interface MyWebService {
@WebResult(name = "res")//用于指定返回值名称,没有此注解 返回值名字为return
public String testMyService(@WebParam(name = "name") String name);//WebParam表示方法的参数,如果不加此注解,方法的参数都从arg0,开始,随着参数增多,name不断增加为arg1,arg2…
}
3 接口实现类
package com.example.service.impl;
import com.example.service.MyWebService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
* @author A
* @date 2023-01-31 12:11:21
* @description
*/
@WebService(name = "myService",//向外暴露的服务名称
targetNamespace = "http://service.example.com",//与接口中的命名空间一致
endpointInterface = "com.example.service.MyWebService"//接口全限定名,注意不是实现类
)
@Component
public class MyWebServiceImpl implements MyWebService {
@Override
public String testMyService(String name) {
return name+"的第一个webSerivce服务";
}
}
4 cxf配置文件
package com.example.config;
import com.example.service.impl.MyWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
//默认servlet路径/*,可自定义
@Bean
public ServletRegistrationBean disServlet() {//此bean名不可为dispatcherServlet,否则启动时会异常
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");//服务访问地址为ip:端口/webService
return servletRegistrationBean;
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint1(){
EndpointImpl endpoint = new EndpointImpl(springBus(), new MyWebServiceImpl());
endpoint.publish("/myService");//这个webService的访问地址
return endpoint;
}
}
5 启动springboot项目
浏览器访问地址:localhost:端口/webService,会列出所有服务
看到这个说明服务发布成功。
测试发布的服务
我用的是SoapUI工具进行webService服务测试
点击运行后的返回值是正确的
客户端调用
新建一个springBoot项目,引用上面的cxf依赖
客户端调用代码如下
System.out.println("开始调用webService服务--------");
// 1、创建 CXF 客户端工厂 Bean 对象
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// 2、创建客户端对象,参数为webService服务地址
org.apache.cxf.endpoint.Client client = dcf.createClient("http://127.0.0.1:8082/webService/myService?wsdl");
// 3、调用接口方法
//第一个参数为方法名,第二个参数为调用这个方法要传递的参数
Object[] objects = client.invoke("testMyService", "程长新");
System.out.println("返回结果:---");
if (objects == null || objects.length == 0){
System.out.println("调用结果为空");
}
System.out.println(objects[0].toString());
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤