当前位置:首页 » 《资源分享》 » 正文

Spring-boot整合Webservice服务端

7 人参与  2024年12月28日 16:01  分类 : 《资源分享》  评论

点击全文阅读


Spring Boot整合Webservice服务端

本文是基于前辈一顿吃不饱的文章SpringBoot整合WebService(服务端+客户端)-CSDN博客,由于工作需要用.NET调用其他系统发布的WebService服务,尝试用java搭建一个WebService服务端测试一下,在此记录。

pom.xml

注意springboot版本和CXF可能会存在版本不兼容

· Spring Boot 3.x:使用CXF的Jakarta版本
· Spring Boot 2.x:使用CXF的Java EE版本

本文是基于Spring Boot 2.7.9参考博主构建的,完整依赖如下:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.7.9</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>org.cqw</groupId>    <artifactId>WebService</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>WebService</name>    <description>WebService</description>    <url/>    <licenses>        <license/>    </licenses>    <developers>        <developer/>    </developers>    <scm>        <connection/>        <developerConnection/>        <tag/>        <url/>    </scm>    <properties>        <java.version>17</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <!--  lombok: 日志 + 实体操作     -->        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <optional>true</optional>        </dependency>        <!-- 以下包和 webservice 相关 -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web-services</artifactId>        </dependency>        <!--   CXF     -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>3.2.0</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>3.2.0</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-core</artifactId>            <version>3.3.5</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>3.2.4</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
创建WebService接口
package org.cqw.webservice.service;import javax.jws.WebService;@WebService(name = "UserService", // 暴露服务名称        targetNamespace = "http://service.webservice.cqw.org"// 命名空间,一般是接口的包名倒序)public interface UserService {    String testWebService();}
创建接口实现类
package org.cqw.webservice.service;import javax.jws.WebService;@WebService(serviceName = "UserService", // 与接口中@WebService指定的name一致        targetNamespace = "http://service.webservice.cqw.org", // 与接口中的命名空间一致,一般是接口的包名倒序        endpointInterface = "org.cqw.webservice.service.UserService"// 接口地址)@Slf4jpublic class UserServiceImpl implements UserService {        public String testWebService() {    log.info("我被调用了");        return "Hello";    }    }
在项目路径(本文是/src/main/java/org/cqw)下创建config文件夹,创建配置类
package org.cqw.webservice.config;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.cqw.webservice.service.UserService;import org.cqw.webservice.service.UserServiceImpl;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;@Configurationpublic class StartClas {    @Bean(name = Bus.DEFAULT_BUS_ID)    public SpringBus springBus() {        return new SpringBus();    }    @Bean(name = "wsBean")    public ServletRegistrationBean dispatcherServlet() {        ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/ws/*");        return wbsServlet;    }    @Bean    public UserService userService() {        return new UserServiceImpl();    }    @Bean    public Endpoint endpointPurchase(SpringBus springBus, UserService userService) {        EndpointImpl endpoint = new EndpointImpl(springBus(), userService());        endpoint.publish("/api");        log.info("服务发布成功!地址为:http://localhost:8081/ws/api?wsdl");        return endpoint;    }}
运行项目,结果如下
在这里插入图片描述

浏览器访问:http://localhost:8080/ws/api?wsdl

在这里插入图片描述


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/208334.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 手术时,我看着病人惨死最新试读_淼淼陆知衍姜颜全本完结_小说后续在线阅读_无删减免费完结_
  • 男友霸道给我开黑卡,转头却骂我是捞女最新章节_肖年顾客黑卡热文_小说后续在线阅读_无删减免费完结_
  • 他在回忆尽头全集_贺南舟许清梨叶絮完结txt_小说后续在线阅读_无删减免费完结_
  • 旅游结婚那天未婚夫另娶女秘书,我让他们一无所有连载_老公迎宾超长版_小说后续在线阅读_无删减免费完结_
  • 完结文异界修仙我的直播间能打赏核弹列表_完结文异界修仙我的直播间能打赏核弹(江流年沈红菱)
  • 全书浏览陪弟弟混骑行圈,离婚你哭什么?(韩星河柳千雪)_陪弟弟混骑行圈,离婚你哭什么?(韩星河柳千雪)全书结局
  • 老公出轨我助攻虐心反转_秦绍卿卿阿溪后续加长_小说后续在线阅读_无删减免费完结_
  • 替老婆坐牢出狱那天,我被送去斗兽场精校文本_许言沈宇郑子番茄热门_小说后续在线阅读_无删减免费完结_
  • 沐巍澜温溪:结局+番外(爱如一刹花火)电子书畅享阅读无广告
  • 傅凌川林柚续集(傅凌川林柚)章节前文+全书阅读(替嫁燃婚:小祖宗嘴硬腰软)最新连载
  • 资助生把导盲犬做成火锅后,我家杀疯了!列表_资助生把导盲犬做成火锅后,我家杀疯了!(陈米雪波波)
  • 沈瑾钰林沐橙豪门废妻浴火复仇续集(豪门废妻浴火复仇)章节在线阅读最近章节

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1