使用Spring Boot创建SOAP Web服务 - NgDeveloper

栏目: Java · 发布时间: 7年前

内容简介:使用Spring Boot创建SOAP Web服务:1. Spring Boot主应用文件:2.pom.xml

使用Spring Boot创建SOAP Web服务:

1. Spring Boot主应用文件:

<b>package</b> com.ngdeveloper;

<b>import</b> org.springframework.boot.SpringApplication;
<b>import</b> org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
<b>public</b> <b>class</b> CouponApplication {

<b>public</b> <b>static</b> <b>void</b> main(String[] args) {
SpringApplication.run(CouponApplication.<b>class</b>, args);
}
}

2.pom.xml

下面的pom.xml文件包含从xsd文件生成输出目录文件的插件,于是会创建pom.xml,xsd文件,端点然后通过启动spring boot应用程序,您将能够找到生成的其他所需文件。

<font><i>//</i></font><font>
</font><font><i>// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7</i></font><font>
</font><font><i>// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a></i></font><font>
</font><font><i>// Any modifications to this file will be lost upon recompilation of the source schema.</i></font><font>
</font><font><i>// Generated on: 2019.01.23 at 03:41:28 PM IST</i></font><font>
</font><font><i>//</i></font><font>
</font>
  • CouponDetails
  • GetCouponDetailsRequest
  • GetCouponDetailsResponse
  • ObjectFactory
  • package-info
<?xml version=<font>"1.0"</font><font> encoding=</font><font>"UTF-8"</font><font>?>
<project xmlns=</font><font>"http://maven.apache.org/POM/4.0.0"</font><font>
xmlns:xsi=</font><font>"http://www.w3.org/2001/XMLSchema-instance"</font><font>
xsi:schemaLocation=</font><font>"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</font><font>>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.ngdeveloper</groupId>
<artifactId>Coupon</artifactId>
<version>1.0</version>
<name>Coupon</name>
<description>Coupon Web service</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<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>
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
</plugins>
</build>

</project>
</font>

为您的请求和响应创建一个xsd文件,

以下coupon-details.xsd文件是请求和响应文件:

<xs:schema xmlns:xs=<font>"http://www.w3.org/2001/XMLSchema"</font><font> xmlns:tns=</font><font>"http://ngdeveloper.com/coupon"</font><font>
targetNamespace=</font><font>"http://ngdeveloper.com/coupon"</font><font> elementFormDefault=</font><font>"qualified"</font><font>>

<xs:element name=</font><font>"GetCouponDetailsRequest"</font><font>>
<xs:complexType>
<xs:sequence>
<xs:element name=</font><font>"id"</font><font> type=</font><font>"xs:int"</font><font> />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name=</font><font>"GetCouponDetailsResponse"</font><font>>
<xs:complexType>
<xs:sequence>
<xs:element name=</font><font>"CouponDetails"</font><font> type=</font><font>"tns:CouponDetails"</font><font> />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name=</font><font>"CouponDetails"</font><font>>
<xs:sequence>
<xs:element name=</font><font>"id"</font><font> type=</font><font>"xs:int"</font><font> />
<xs:element name=</font><font>"title"</font><font> type=</font><font>"xs:string"</font><font> />
<xs:element name=</font><font>"code"</font><font> type=</font><font>"xs:string"</font><font> />
</xs:sequence>
</xs:complexType>

</xs:schema>
</font>

3.创建端点:

<b>package</b> com.ngdeveloper.ws.endpoints;

<b>import</b> org.springframework.ws.server.endpoint.annotation.Endpoint;
<b>import</b> org.springframework.ws.server.endpoint.annotation.PayloadRoot;
<b>import</b> org.springframework.ws.server.endpoint.annotation.RequestPayload;
<b>import</b> org.springframework.ws.server.endpoint.annotation.ResponsePayload;

<b>import</b> com.ngdeveloper.CouponDetails;
<b>import</b> com.ngdeveloper.GetCouponDetailsRequest;
<b>import</b> com.ngdeveloper.GetCouponDetailsResponse;

@Endpoint
<b>public</b> <b>class</b> CouponDetailsEndPoint {

@PayloadRoot(namespace = <font>"http://ngdeveloper.com/coupon"</font><font>, localPart = </font><font>"GetCouponDetailsRequest"</font><font>)
@ResponsePayload
<b>public</b> GetCouponDetailsResponse processCouponDetailsRequest(@RequestPayload GetCouponDetailsRequest request) {
GetCouponDetailsResponse response = <b>new</b> GetCouponDetailsResponse();
CouponDetails couponDetails = <b>new</b> CouponDetails();
couponDetails.setId(request.getId());
couponDetails.setTitle(</font><font>"10% OFF on Paytm"</font><font>);
couponDetails.setCode(</font><font>"PYTM10"</font><font>);
response.setCouponDetails(couponDetails);
<b>return</b> response;
}
}
</font>

4.WebserviceConfig文件:

这个配置文件在 spring boot for soap webservice and load the xsd file

<b>package</b> com.ngdeveloper;

<b>import</b> org.springframework.boot.web.servlet.ServletRegistrationBean;
<b>import</b> org.springframework.context.ApplicationContext;
<b>import</b> org.springframework.context.annotation.Bean;
<b>import</b> org.springframework.context.annotation.Configuration;
<b>import</b> org.springframework.core.io.ClassPathResource;
<b>import</b> org.springframework.ws.config.annotation.EnableWs;
<b>import</b> org.springframework.ws.config.annotation.WsConfigurerAdapter;
<b>import</b> org.springframework.ws.transport.http.MessageDispatcherServlet;
<b>import</b> org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
<b>import</b> org.springframework.xml.xsd.SimpleXsdSchema;
<b>import</b> org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
<b>public</b> <b>class</b> WebServiceConfig <b>extends</b> WsConfigurerAdapter {
@Bean
<b>public</b> ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = <b>new</b> MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(<b>true</b>);
<b>return</b> <b>new</b> ServletRegistrationBean(servlet, <font>"/ws/*"</font><font>);
}

@Bean(name = </font><font>"coupon"</font><font>)
<b>public</b> DefaultWsdl11Definition defaultCouponWsdl11Definition(XsdSchema couponSchema) {
DefaultWsdl11Definition wsdl11Definition = <b>new</b> DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName(</font><font>"CouponPort"</font><font>);
wsdl11Definition.setLocationUri(</font><font>"/ws"</font><font>);
wsdl11Definition.setTargetNamespace(</font><font>"http://ngdeveloper.com/coupon"</font><font>);
wsdl11Definition.setSchema(couponSchema);
<b>return</b> wsdl11Definition;
}

@Bean
<b>public</b> XsdSchema couponSchema() {
<b>return</b> <b>new</b> SimpleXsdSchema(<b>new</b> ClassPathResource(</font><font>"coupon-details.xsd"</font><font>));
}
}
</font>

在application.properties中添加以下行以记录输入请求和输出响应,

logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.server.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.received=TRACE
logging.level.org.springframework.ws.server.MessageTracing.received=TRACE

测试创建的SOAP Web服务:

安装 wizdler插件 以直接从chrome浏览器测试SOAP Web服务。

您还可以使用 SOAP UI工具 (此处提供免费和专业工具)

在chrome浏览器中打开wsdl链接,然后单击wizdler图标以传递请求


以上所述就是小编给大家介绍的《使用Spring Boot创建SOAP Web服务 - NgDeveloper》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Natural Language Processing with Python

Natural Language Processing with Python

Steven Bird、Ewan Klein、Edward Loper / O'Reilly Media / 2009-7-10 / USD 44.99

This book offers a highly accessible introduction to Natural Language Processing, the field that underpins a variety of language technologies, ranging from predictive text and email filtering to autom......一起来看看 《Natural Language Processing with Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试