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", //命名空間 指定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<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();
    }
}

編譯轉換

將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=張三

此時就調用成功嘍

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM