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

Spring-boot整合Webservice服务端

24 人参与  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)
  • 赞助本站

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

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

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