如何使用spring-ws发布webservice服务

 更新时间:2024年11月11日 10:12:34   作者:昱禹  
文章介绍了如何使用Spring-WS发布Web服务,包括添加依赖、创建XSD文件、生成JAXB实体、配置Endpoint、启动服务等步骤,结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

spring-ws

添加依赖、插件

pom.xml添加

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
<!--            要用的时候把下面插件打开, 不用了记得注释-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>${project.basedir}/src/main/resources/xsd/fakeBindUrl.xsd</source>
                    </sources>
                </configuration>
            </plugin>

创建XSD文件

对比实际输入输出参考修改xsd

实际输入、输出 输入

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:snr="snr">
<soapenv:Header/>
<soapenv:Body>
<snr:bindRequest soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<RequestInfo xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<Root>
<Domain>host</Domain>
<Passwd>pasdasjidojoi</Passwd>
<SrvCode>489489489445645648</SrvCode>
<Content>
<![CDATA[<?xml version='1.0' encoding='UTF-8'?><FakePassQuery><FakeCode>admin</FakeCode><Password><![CDATA[dsaiodas54545]]]]><![CDATA[></Password><FakeType>1000</FakeType></FakePassQuery>]]>
</Content>
</Root>
</RequestInfo></snr:bindRequest></soapenv:Body></soapenv:Envelope>

输出

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <FakePassQueryResponse>
            <ExchangeId>a79bc02ea21a4a13a7c58108cc864a9d</ExchangeId>
            <ErrorCode>00000</ErrorCode>
            <IsSuccess>T</IsSuccess>
            <FakeContent>
                <FakeId>1</FakeId>
                <FakeCode>admin</FakeCode>
                <StaffName>admin</StaffName>
                <OrgId/>
                <EffDate>2024-01-01 00:00:00</EffDate>
                <ExpDate>2125-01-08 13:28:52</ExpDate>
                <StatusCd>0</StatusCd>
                <ContactTel/>
                <SmsTel/>
            </FakeContent>
        </FakePassQueryResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service"
           targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified">
    <xs:element name="bindRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="RequestInfo" type="tns:RequestInfo"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="RequestInfo">
        <xs:sequence>
            <xs:element name="Root" type="tns:Root"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Root">
        <xs:sequence>
            <xs:element name="Domain" type="xs:string"/>
            <xs:element name="Passwd" type="xs:string"/>
            <xs:element name="SrvCode" type="xs:string"/>
            <xs:element name="Content" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="FakePassQuery">
        <xs:sequence>
            <xs:element name="FakeCode" type="xs:string"/>
            <xs:element name="Password" type="xs:string"/>
            <xs:element name="FakeType" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="FakePassQueryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="ErrorInfo" type="xs:string"/>
                <xs:element name="ResultCode" type="xs:string"/>
                <xs:element name="ExchangeId" type="xs:string"/>
                <xs:element name="ErrorCode" type="xs:string"/>
                <xs:element name="IsSuccess" type="xs:string"/>
                <xs:element name="FakeContent" type="tns:FakeContent"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="FakeContent">
        <xs:sequence>
            <xs:element name="FakeId" type="xs:string"/>
            <xs:element name="FakeCode" type="xs:string"/>
            <xs:element name="StaffName" type="xs:string"/>
            <xs:element name="OrgId" type="xs:string"/>
            <xs:element name="EffDate" type="xs:string"/>
            <xs:element name="ExpDate" type="xs:string"/>
            <xs:element name="StatusCd" type="xs:string"/>
            <xs:element name="ContactTel" type="xs:string"/>
            <xs:element name="SmsTel" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

生成xsd实体

idea -> maven -> 插件 -> jaxb2 -> jaxb2:xjc
生成的文件在:target\generated-sources\jaxb

配置

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    }
    // 这里的BeanName就是实际访问路径,当前服务请求路径: ip:port/ws/fakeBindUrl
    @Bean(name = "fakeBindUrl")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema subAcctInfoForSelfBindSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountriesPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("snr");
        wsdl11Definition.setSchema(subAcctInfoForSelfBindSchema);
        return wsdl11Definition;
    }
    @Bean
    public XsdSchema subAcctInfoForSelfBindSchema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/fakeBindUrl.xsd"));
    }
    // 多个webservice服务,则注册多个wsdl11Definition、XsdSchema
    // 这里的BeanName就是实际访问路径,当前这个就是/ws/fakeBindUrl2
    // @Bean(name = "fakeBindUrl2")
    // public DefaultWsdl11Definition defaultWsdl11Definition2(XsdSchema subAcctInfoForSelfBindSchema2) {
    // 	DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    // 	wsdl11Definition.setPortTypeName("CountriesPort");
    // 	wsdl11Definition.setLocationUri("/ws");
    // 	wsdl11Definition.setTargetNamespace("snr");
    // 	wsdl11Definition.setSchema(subAcctInfoForSelfBindSchema);
    // 	return wsdl11Definition;
    // }
    // @Bean
    // public XsdSchema subAcctInfoForSelfBindSchema2() {
    // 	return new SimpleXsdSchema(new ClassPathResource("xsd/fakeBindUrl.xsd"));
    // }
}

定义Endpoint

@Endpoint
@Slf4j
public class bindRequestEndpoint {
    private static final String NAMESPACE_URI = "snr";
    @Autowired
    private SubAcctBindingService subAcctBindingService;
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "bindRequest")
    @ResponsePayload
    public FakePassQueryResponse fakeBindUrl(@RequestPayload bindRequest requestInfo) {
        try {
            log.info("收到请求: {}", requestInfo);
            // 解析requestInfo的XML内容进行业务处理
            FakePassQuery FakePassQuery = parseInnerXML(requestInfo.getRequestInfo().getRoot().getContent());
            FakePassQueryResponse result = subAcctBindingService.processBindRequest(FakePassQuery);
            return result;
        } catch (Exception e) {
            log.error("处理请求时发生异常: ", e);
            // 组装错误响应
            FakePassQueryResponse response = new FakePassQueryResponse();
            response.setErrorInfo(e.getMessage());
            response.setResultCode("-1");
            response.setIsSuccess("F");
            return response;
        }
    }
}

启动服务

当前服务的ip端口号
ip:port/ws/fakeBindUrl?wsdl

其他

如遇报错可以在入参中尝试加入namespace

@XmlRootElement(namespace="", ...)

1 counts of IllegalAnnotationExceptions

入参或出参定义有问题
@XmlTypepropOrder与实际属性不符等等

到此这篇关于使用spring-ws发布webservice服务的文章就介绍到这了,更多相关Spring AI Alibaba百炼平台大模型内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springmvc使用JSR-303进行数据校验实例

    springmvc使用JSR-303进行数据校验实例

    本篇文章主要介绍了详解springmvc使用JSR-303进行数据校验,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-02-02
  • 浅谈Java中ThreadLocal引发的内存泄漏

    浅谈Java中ThreadLocal引发的内存泄漏

    本文主要介绍了浅谈Java中ThreadLocal引发的内存泄漏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java web拦截器inteceptor原理及应用详解

    Java web拦截器inteceptor原理及应用详解

    这篇文章主要介绍了java web拦截器inteceptor原理及应用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • XXL-Job端口额外占用问题的解决方法小结

    XXL-Job端口额外占用问题的解决方法小结

    最近博主在Spring整合XXL-JOB到项目时发现了个问题,注册执行器需要额外占用端口,也就是我们每启动一个程序,除了程序本身的API端口外,还需要额外开放一个执行器端口,所以本文给大家分享了XXL-Job端口额外占用问题的解决方法小结,需要的朋友可以参考下
    2024-05-05
  • Java切面(Aspect)的多种实现方式

    Java切面(Aspect)的多种实现方式

    这篇文章主要给大家介绍了关于Java切面(Aspect)的多种实现方式,在Java开发中切面(Aspect)是一种常用的编程方式,用于实现横切关注点(cross-cutting concern),需要的朋友可以参考下
    2023-08-08
  • Spring MVC基于注解的使用之JSON数据处理的方法

    Spring MVC基于注解的使用之JSON数据处理的方法

    这篇文章主要介绍了Spring MVC基于注解的使用JSON数据处理,json是一种轻量级的数据交换格式,是一种理想的数据交互语言,它易于阅读和编写,同时也易于机器解析和生成,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • mall整合SpringSecurity及JWT认证授权实战下

    mall整合SpringSecurity及JWT认证授权实战下

    这篇文章主要为大家介绍了mall整合SpringSecurity及JWT认证授权实战第二篇,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Java实现的百度语音识别功能示例

    Java实现的百度语音识别功能示例

    这篇文章主要介绍了Java实现的百度语音识别功能,较为简明扼要的分析了Java调用百度语音接口相关操作步骤,并给出了具体的语音识别用法代码示例,需要的朋友可以参考下
    2018-08-08
  • IDEA + Maven环境下的SSM框架整合及搭建过程

    IDEA + Maven环境下的SSM框架整合及搭建过程

    这篇文章主要介绍了IDEA + Maven环境下的SSM框架整合及搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • JAVA实现多线程的两种方法实例分享

    JAVA实现多线程的两种方法实例分享

    这篇文章介绍了JAVA实现多线程的两种方法实例分享,有需要的朋友可以参考一下
    2013-08-08

最新评论