SpringBoot整合WebService(实用版)


SpringBoot整合WebService

简介

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

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

代码示例

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

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


    org.springframework.boot
    spring-boot-starter-parent
    2.5.0
     

Server搭建

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

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


    
        org.apache.cxf
        cxf-spring-boot-starter-jaxws
        3.2.5
    
    
    
        org.hibernate
        hibernate-validator
        6.0.18.Final
    

    
        org.projectlombok
        lombok
    

启动类(引导类)

@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", //命名空间 指定wsdl说明书在Client端所存放的包路径为com.aerfazhe.wsdl包下
        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 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


    
        org.apache.cxf
        cxf-spring-boot-starter-jaxws
        3.2.5
    
    
    
        org.hibernate
        hibernate-validator
        6.0.18.Final
    

    
        org.projectlombok
        lombok
    



    
        
            org.apache.cxf
            cxf-codegen-plugin
            3.2.5
            
                
                    generate-sources
                    generate-sources
                    
                        src/main/resources/cxf
                        
                            
                                http://localhost:8080/ws/user?wsdl
                            
                        
                    
                    
                        wsdl2java
                    
                
            
        
    

配置类

@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客户端项目根路径下调出控制台,如下

使用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 

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

此时就调用成功喽