SpringBoot整合WebService(实用版)

SpringBoot整合WebService

简介

WebService就是一种跨编程语言和跨操作系统平台的远程调用技术

此处就不赘述WebService相关概念和原理了,可以参考:https://blog.csdn.net/c99463904/article/details/76018436

代码示例

此处共分两个端,客户端和服务端,一个负责调用接口,一个负责创建接口并实现

首先创建一个Maven父项目,在pom.xml文件中引入SpringBoot坐标

<parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.5.0</version>
    <relativepath> <!-- lookup parent from repository -->
</relativepath></parent>

Server搭建

在父项目中创建服务端(Server)子项目,

pom.xml文件,引入以下坐标

<dependencies>
    <dependency>
        <groupid>org.apache.cxf</groupid>
        <artifactid>cxf-spring-boot-starter-jaxws</artifactid>
        <version>3.2.5</version>
    </dependency>
    <!--    不引入会报错  报接口未实现  -->
    <dependency>
        <groupid>org.hibernate</groupid>
        <artifactid>hibernate-validator</artifactid>
        <version>6.0.18.Final</version>
    </dependency>

    <dependency>
        <groupid>org.projectlombok</groupid>
        <artifactid>lombok</artifactid>
    </dependency>
</dependencies>

启动类(引导类)

@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class,args);
    }

}

User.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private String name;

    private Integer age;

}

UserService接口

@WebService(targetNamespace = "wsdl.aerfazhe.com",name = "userPortType")
public interface UserService {

    @WebMethod
    User getUserName(@WebParam(name = "name") String name);

}

UserService实现类

@WebService(
        targetNamespace = "wsdl.aerfazhe.com", //&#x547D;&#x540D;&#x7A7A;&#x95F4; &#x6307;&#x5B9A;wsdl&#x8BF4;&#x660E;&#x4E66;&#x5728;Client&#x7AEF;&#x6240;&#x5B58;&#x653E;&#x7684;&#x5305;&#x8DEF;&#x5F84;&#x4E3A;com.aerfazhe.wsdl&#x5305;&#x4E0B;
        name = "userPortType",
        serviceName = "userService", //&#x670D;&#x52A1;Name&#x540D;&#x79F0;
        portName = "userPortName",
        endpointInterface = "com.aerfazhe.service.UserService" // &#x6307;&#x5B9A;webService&#x7684;&#x63A5;&#x53E3;&#x7C7B;&#xFF0C;&#x6B64;&#x7C7B;&#x4E5F;&#x9700;&#x8981;&#x63A5;&#x5165;@WebService&#x6CE8;&#x89E3;
)
public class UserServiceImpl implements UserService {

    @Override
    public User getUserName(String name) {
        User user = new User(name, 28);
        return user;
    }

}

配置类

@Configuration
public class CxfWebServiceConfig {

    @Bean("cxfServletRegistration")
    public ServletRegistrationBean<cxfservlet> dispatcherServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(),"/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    }

    @Bean
    public Endpoint endpoint(UserService userService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
        endpoint.publish("/user");
        return endpoint;
    }

}</cxfservlet>

启动后访问wsdl说明书

http:localhost:8080/ws/user?wsdl

SpringBoot整合WebService(实用版)

Client搭建

pom.xml

<dependencies>
    <dependency>
        <groupid>org.apache.cxf</groupid>
        <artifactid>cxf-spring-boot-starter-jaxws</artifactid>
        <version>3.2.5</version>
    </dependency>
    <!--    不引入会报错  报接口未实现  -->
    <dependency>
        <groupid>org.hibernate</groupid>
        <artifactid>hibernate-validator</artifactid>
        <version>6.0.18.Final</version>
    </dependency>

    <dependency>
        <groupid>org.projectlombok</groupid>
        <artifactid>lombok</artifactid>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupid>org.apache.cxf</groupid>
            <artifactid>cxf-codegen-plugin</artifactid>
            <version>3.2.5</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceroot>src/main/resources/cxf</sourceroot>
                        <wsdloptions>
                            <wsdloption>
                                <wsdl>http://localhost:8080/ws/user?wsdl</wsdl>
                            </wsdloption>
                        </wsdloptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置类

@Configuration
public class CxfClientConfig {

    private final static String SERVICE_ADDRESS = "http://localhost:8080/ws/user";

    @Bean("cxfProxy")
    public UserPortType createUserPortTypeProxy() {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setAddress(SERVICE_ADDRESS);
        jaxWsProxyFactoryBean.setServiceClass(UserPortType.class);
        return (UserPortType) jaxWsProxyFactoryBean.create();
    }
}

编译转换

将XML格式的wsdl说明书转化为Java对象格式

在Client客户端项目根路径下调出控制台,如下

SpringBoot整合WebService(实用版)

使用maven命令进行转换,生成的Java代码在Client客户端下的src/main/resources/cxf目录下

mvn generate-sources

查看src/main/resources/cxf 下的文件,如下

创建com/aerfazhe/wsdl包路径,将这些JavaObject剪切到该包路径下,如下

Controller

@RestController
public class UserController {

    @Resource(name = "cxfProxy")
    private UserPortType userPortType;

    @GetMapping("/getUserName")
    public User getUserName(String name) {
        User user = userPortType.getUserName(name);
        return user;
    }

}

启动类

@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}

application.yml

server:
  port: 8081

在当前项目根目录下,输入以下命令,将xml类型的wsdl说明书转化为Java对象

mvn generate-sources

SpringBoot整合WebService(实用版)

启动后访问:http://localhost:8081/getUserName?name=张三

SpringBoot整合WebService(实用版)

此时就调用成功喽

Original: https://www.cnblogs.com/aerfazhe/p/16093653.html
Author: 阿尔法哲
Title: SpringBoot整合WebService(实用版)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/643759/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球