spring boot + spring ws 实现webservice服务发布

spring-webservice

介绍

使用spring boot + spring ws 实现webservice服务发布

创建工程

创建spring boot工程,添加依赖

    <dependency>
           <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
    </dependency>
生成schema

Spring Web Services (Spring-WS)专注于创建文档驱动的 Web 服务。其核心思想是通过文档来创建生成web服务,这便要求根据服务的需求确定入参和返回值来生成对相应的xsd文件。如:一个普通登录服务,输入用户名 + 密码 + 验证码,返回成功状态码 + 菜单。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://yqc.com/ws/login"
           targetNamespace="http://yqc.com/ws/login" elementFormDefault="qualified">
    <!-- 注意要求请求和返回对象的前缀一致,各自加Request和Response后缀  loginRequest loginResponse-->
    <!--如果对象不以Request和Response对象为后缀,则生成的wsdl文件中 wsdl:operation这些标签 -->
    <!--如果请求名和返回名不一致,如 loginRequest 和 returnResponse 则从wsdl生成代码时,服务方法没有返回值  -->
    <!--定义登录表单请求对象 -->
    <xs:element name="loginRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="password" type="xs:string"/>
                <xs:element name="msgcode" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!--定义返回数据元素 -->
    <xs:element name="loginResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="result" type="tns:resData"/>
                <xs:element name="menuData" type="tns:menuList"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="menuList">
        <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="unbounded" name="menu" nillable="true" type="tns:menuItem"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="menuItem">
        <xs:sequence>
            <xs:element name="menuId" type="xs:string"/>
            <xs:element name="menuName" type="xs:string"/>
            <xs:element name="menuOrder" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="resData">
        <xs:sequence>
            <xs:element name="code" type="xs:string"/>
            <xs:element name="msg" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>
自动生成代码

1、根据定义的xsd文件,利用JAXB2(将一个Java对象转变成为XML格式,反之亦然)生成对用实体模型,可以下载trang.jar文件,根据xsd生成对应实体。(此处不推荐)

# 将xml转成xsd
java -jar trang.jar book.xml book.xsd
# 将 xsd生成到包下
xjc -p com.bean text.xsd -d D:\wsdl

2、添加JAXB2插件,执行maven install 命令生成实体。

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- xsd所在目录 -->
                    <schemaDirectory>
                        ${project.basedir}/src/main/resources/xsd/
                    </schemaDirectory>
                    <!-- 生成目录 -->
                    <outputDirectory>
                        ${project.basedir}/src/main/java/
                    </outputDirectory>
                    <clearOutputDir>
                        false
                    </clearOutputDir>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
创建端点
@Endpoint
public class LoginEndpoint {

    // 此处应与xsd文件中定义的保持一致
    private static final String NAMESPACE_URI = "http://yqc.com/ws/login";

    @Autowired
    private LoginService loginService;


    // localPart的值对应xsd中参数的标签的name值
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "loginRequest")
    @ResponsePayload
    public LoginResponse login(@RequestPayload LoginRequest form){
        return loginService.login(form);
    }

}
配置服务
// 开启webservice服务
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    /**
     * bean 名称是对外发布的wsdl的名称
     * @param loginSchema
     * @return
     */
    @Bean(name = "login")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema loginSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
//        wsdl11Definition.setServiceName("loginName");
        wsdl11Definition.setPortTypeName("loginPort");
        wsdl11Definition.setLocationUri("/ws");
        // 默认后缀为Request 和 Response, 自定义需在此处声明
//        wsdl11Definition.setRequestSuffix();
//        wsdl11Definition.setTargetNamespace("http://yqc.com/ws/login");
        wsdl11Definition.setSchema(loginSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema loginSchema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/login.xsd"));
    }
}
添加springboot启动类
@SpringBootApplication
public class WsApplication {
    public static void main(String[] args) {
        SpringApplication.run(WsApplication.class, args);
    }
}

服务端至此结束,可访问http://localhost:7474/ws/login.wsdl查看。

生成客户端代码

在这里插入图片描述

在这里插入图片描述

点击生成客户端代码

客户端测试代码
public class Test {
    public static void main(String[] args) {
        LoginPortService service = new LoginPortService();
        LoginPort loginPort = service.getLoginPortSoap11();
        LoginRequest request = new LoginRequest();
        request.setName("张三");
        request.setPassword("12312");
        LoginResponse response = loginPort.login(request);
        System.out.println(response);
    }
}
注意事项
  • 注意要求请求和返回对象的前缀一致,各自加Request和Response后缀 如 loginRequest loginResponse
  • 如果对象不以Request和Response对象为后缀,则生成的wsdl文件中 没有wsdl:operation这些标签。如果使用自定义后缀则需在配置服务时设置。
  • 如果请求名和返回名不一致,如 loginRequest 和 returnResponse 则从wsdl生成代码时,服务方法没有返回值
    在这里插入图片描述
    demo项目地址 https://gitee.com/chaoslover/spring-webservice.git
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值