ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot整合WebService(实用版)

2022-04-02 19:33:16  阅读:240  来源: 互联网

标签:SpringBoot WebService class 实用 user User org public name


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有