SpringBoot集成WebService

news/2024/4/28 18:10:18

1)添加依赖

 <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.3.4</version><exclusions><exclusion><groupId>javax.validation</groupId><artifactId>validation-api</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>com.sun.xml.ws</groupId><artifactId>jaxws-ri</artifactId><version>2.3.3</version><type>pom</type>
</dependency>

2)添加服务接口

public interface UserService {UserDTO getById(@WebParam(name="id") Integer id);
}

3)添加服务实现类

@Service
@WebService(targetNamespace = "http://www.xjs1919.com", serviceName = "userService"
)
public class UserServiceImpl implements UserService {@Overridepublic UserDTO getById(Integer id) {return new UserDTO(id, "hello_"+id);}
}

4)添加配置类

@Configuration
@Slf4j
public class WebServiceConfig {@Autowiredprivate UserService userService;/*** 注入Servlet,注意beanName不能为dispatcherServlet*/@Beanpublic ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), userService);endpoint.publish("/userService");return endpoint;}}

5)启动应用,查看服务列表

浏览器访问:http://localhost:8888/webservice
在这里插入图片描述
点击WSDL后面的连接可以查看WSDL地址和内容:
在这里插入图片描述

6)测试

@Test
public void testGetById(){JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient("http://localhost:8888/webservice/userService?wsdl");final ObjectMapper mapper = new ObjectMapper();try {Object[] objects = client.invoke("getById", 123);System.out.println(mapper.writeValueAsString(objects[0]));} catch (Exception e) {e.printStackTrace();;}
}

完整源码下载:https://github.com/xjs1919/enumdemo/tree/master/springboot-webservice

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10774.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Elastic Stack--09--ElasticsearchRestTemplate

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 spring-data-elasticsearch提供的APIQueryBuildersElasticsearchRestTemplate 方法ElasticsearchRestTemplate ---操作索引 ElasticsearchRestTemplate ---文档操作…

【Leetcode】2684. 矩阵中移动的最大次数

文章目录 题目思路代码结果 题目 题目链接&#x1f517; 给你一个下标从 0 开始、大小为 m x n 的矩阵 grid &#xff0c;矩阵由若干 正 整数组成。 你可以从矩阵第一列中的 任一 单元格出发&#xff0c;按以下方式遍历 grid &#xff1a; 从单元格 (row, col) 可以移动到 (…

K8s-CRD实战

CRD CRD的全称是CustomResourceDefinition,是Kubernetes为提高可扩展性, 让开发者去自定义资源&#xff08;如Deployment&#xff0c;StatefulSet等&#xff09;的一种方法. Controller controller是由controller-manager进行管理&#xff0c;通过API Server提供的接口实时监…

Leetcode 79. 单词搜索

心路历程&#xff1a; 做完这道题才发现是回溯&#xff0c;一开始想的是递归&#xff0c;判断完第i个字符后&#xff0c;只需要挨个判断第i1个字符在不在第i个字符的邻域。后来发现由于不能重复使用元素&#xff0c;所以需要维护一个visited列表&#xff0c;并且在遍历所有可能…

网络安全(黑客)——2024自学

01 什么是网络安全 网络安全可以基于攻击和防御视角来分类&#xff0c;我们经常听到的 “红队”、“渗透测试” 等就是研究攻击技术&#xff0c;而“蓝队”、“安全运营”、“安全运维”则研究防御技术。 无论网络、Web、移动、桌面、云等哪个领域&#xff0c;都有攻与防两面…

python打开文件并读取文件内容(python readlines读取文件内容)

通过open 方法打开 test .py 文件&#xff0c;以读“ r ”的方式打开。通过 调用readlines() 方法逐行的来读取文件。中的数据 try的语句块中&#xff0c;用 for 循环来逐行的打印 test.py 文件中的数据&#xff0c;每循环一次休眠一下。在 finally 语句块中执行文件的 clos…