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

19-01-25 banq
         

使用Spring Boot创建SOAP Web服务:

1. Spring Boot主应用文件:

package com.ngdeveloper;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CouponApplication {

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

2.pom.xml

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

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2019.01.23 at 03:41:28 PM IST
//

  • CouponDetails
  • GetCouponDetailsRequest
  • GetCouponDetailsResponse
  • ObjectFactory
  • package-info

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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>

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

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

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

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

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

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

</xs:schema>

3.创建端点:

package com.ngdeveloper.ws.endpoints;

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.ngdeveloper.CouponDetails;
import com.ngdeveloper.GetCouponDetailsRequest;
import com.ngdeveloper.GetCouponDetailsResponse;

@Endpoint
public class CouponDetailsEndPoint {

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

4.WebserviceConfig文件:

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

package com.ngdeveloper;

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

@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(name = "coupon")
public DefaultWsdl11Definition defaultCouponWsdl11Definition(XsdSchema couponSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("CouponPort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://ngdeveloper.com/coupon");
wsdl11Definition.setSchema(couponSchema);
return wsdl11Definition;
}

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

在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图标以传递请求