其他分享
首页 > 其他分享> > SpringBoot整合WebService(实用版)

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 -->
</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", //命名空间
        name = "userPortType",
        serviceName = "userService", //服务Name名称
        portName = "userPortName",
        endpointInterface = "com.aerfazhe.service.UserService" // 指定webService的接口类,此类也需要接入@WebService注解
)
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;
    }

}

启动后访问wsdl说明书

http:localhost:8080/ws/user?wsdl

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();
    }
}

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 

 

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

测试即可

 

标签:SpringBoot,WebService,class,实用,user,User,org,public,name
来源: https://www.cnblogs.com/aerfazhe/p/16093653.html