SpringBootSOAPWebservice例⼦
阅读⽬录
本⽂将学习如何利⽤Spring boot快速创建SOAP webservice服务;
虽然⽬前REST和微服务越来越流⾏,但是SOAP在某些情况下,仍然有它的⽤武之地;
在本篇 spring boot SOAP教程中,我们会专注于和Spring boot相关的配置,感受下在Spring Boot中,创建SOAP webservice是如何的简便、快速;本⽂将以⼀个"学⽣搜索"这个⼩功能作为⽰例,演⽰Spring Boot中SOAP webservice的创建过程;
JDK 1.8, Eclipse, Maven – 开发环境
Spring-boot – 基础开发框架
wsdl4j – 发布WSDL
SOAP-UI – 测试服务
JAXB maven plugin - 代码⽣成
本⼯程的代码及⽂件⽬录结构如下
访问 ⽹站,添加Web Services依赖,输⼊maven的GAV 坐标,点击下载⼯程,下载完成后解压导⼊IDE即可;
修改l⽂件,添加Wsdl4j依赖:
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
创建student.xsd⽂件,并放到我们⼯程的resources ⽬录下
student.xsd
<xs:schema xmlns:xs="/2001/XMLSchema" xmlns:tns="www.howtodoinjava/xml/school"
targetNamespace="www.howtodoinjava/xml/school" elementFormDefault="qualified">
<xs:element name="StudentDetailsRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="StudentDetailsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="Student" type="tns:Student"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Student">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="standard" type="xs:int"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我们将使⽤jaxb2-maven-plugin来⾼效的⽣成domain代码,⾸先需要在l⽂件添加以下插件配置代码:
<plugin>
<groupId&jo</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>
该插件将使⽤ XJC⼯具作为代码⽣成引擎,XJC能将XML schema ⽂件转成带注解的代码;
现在,我们就可以执⾏以上插件⽣成代码了;
StudentEndpoint类会处理所有访问该服务的请求,并委派给StudentRepository去处理,具体代码如下:ample.howtodoinjava.springbootsoapservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dpoint.annotation.Endpoint;
import org.springframework.dpoint.annotation.PayloadRoot;
import org.springframework.dpoint.annotation.RequestPayload;
import org.springframework.dpoint.annotation.ResponsePayload;
import l.school.StudentDetailsRequest;
import l.school.StudentDetailsResponse;
@Endpoint
public class StudentEndpoint
{
private static final String NAMESPACE_URI = "www.howtodoinjava/xml/school";
private StudentRepository StudentRepository;
@Autowired
public StudentEndpoint(StudentRepository StudentRepository) {
this.StudentRepository = StudentRepository;
}
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "StudentDetailsRequest")
@ResponsePayload
public StudentDetailsResponse getStudent(@RequestPayload StudentDetailsRequest request) { StudentDetailsResponse response = new StudentDetailsResponse();
response.setStudent(StudentRepository.Name()));
return response;
}
}
对上⾯的⼏个注解做个简单说明(可以和Spring MVC的Controller做个类⽐,有点相似):
1. @Endpoint 声明⽤于处理SOAP消息
2. @PayloadRoot 根据namespace和localPart映射对应的处理⽅法
3. @RequestPayload 声明进来的消息将会与该⽅法的参数映射
4. @ResponsePayload ⽅法返回值的映射
出于简便考虑,我们将直接在代码⾥初始化相关数据,代码如下:
创建StudentRepository.java,加上@Repository注解,添加findStudent()⽅法:
ample.howtodoinjava.springbootsoapservice;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import l.school.Student;
@Component
public class StudentRepository {
private static final Map<String, Student> students = new HashMap<>();
@PostConstruct
public void initData() {
Student student = new Student();
student.setName("Sajal");
调用webservice服务student.setStandard(5);
student.setAddress("Pune");
students.Name(), student);
student = new Student();
student.setName("Kajal");
student.setStandard(5);
student.setAddress("Chicago");
students.Name(), student);
student = new Student();
student.setName("Lokesh");
student.setStandard(6);
student.setAddress("Delhi");
students.Name(), student);
student = new Student();
student.setName("Sukesh");
student.setStandard(7);
student.setAddress("Noida");
students.Name(), student);
}
public Student findStudent(String name) {
(name);
}
}
添加⼀个带 @Configuration注解的配置类:
ample.howtodoinjava.springbootsoapservice;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import t.ApplicationContext;
import t.annotation.Bean;
import t.annotation.Configuration;
import io.ClassPathResource;
import org.fig.annotation.EnableWs;
import org.fig.annotation.WsConfigurerAdapter;
import org.ansport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import l.xsd.SimpleXsdSchema;
import l.xsd.XsdSchema;
@EnableWs
@Configuration
public class Config extends WsConfigurerAdapter
{
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext)
{
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/service/*");
}
@Bean(name = "studentDetailsWsdl")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema)
{
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("StudentDetailsPort");
wsdl11Definition.setLocationUri("/service/student-details");
wsdl11Definition.setTargetNamespace("www.howtodoinjava/xml/school");
wsdl11Definition.setSchema(countriesSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema countriesSchema()
{
return new SimpleXsdSchema(new ClassPathResource("school.xsd"));
}
}
该类继承了WsConfigurerAdapter类配置了注解驱动的 Spring-WS编程模式;
MessageDispatcherServlet - Spring-WS使⽤该类处理SOAP 请求,我们需要把该Servlet注⼊ApplicationContext ,以便Spring-WS能到其它Bean;
DefaultWsdl11Definition 使⽤XsdSchema暴露了⼀个标准的的WSDL 1.1,bean的名字studentDetailsWsdl 将会作为wsdl 暴露出去的名称,我们可以通过localhost:8080/service/studentDetailsWsdl.wsdl路径访问;
使⽤mvn clean install maven命名构建⼯程,并使⽤java -jar target\spring-boot-soap-service-0.0.1-SNAPSHOT.jar命令启动应⽤;
执⾏完以上操作后,将会以默认的8080端⼝启动⼀个tomcat服务,本应⽤将部署在该服务⾥;
如果我们的wsdl没问题的话,我们可以使⽤该WSDL 在SOAP ui ⾥创建⼀个⼯程,并测试该应⽤,请求和响应⽰例如下:
请求:
<soapenv:Envelope xmlns:soapenv="/soap/envelope/" xmlns:sch="www.howtodoinjava/xml/school">
<soapenv:Header/>
<soapenv:Body>
<sch:StudentDetailsRequest>
<sch:name>Sajal</sch:name>
</sch:StudentDetailsRequest>
</soapenv:Body>
</soapenv:Envelope>
响应
<SOAP-ENV:Envelope xmlns:SOAP-ENV="/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:StudentDetailsResponse xmlns:ns2="www.howtodoinjava/xml/school">
<ns2:Student>
<ns2:name>Sajal</ns2:name>
<ns2:standard>5</ns2:standard>
<ns2:address>Pune</ns2:address>
</ns2:Student>
</ns2:StudentDetailsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
本⽂学习了如何使⽤Spring Boot创建SOAP webservice,同时也学习了如何利⽤wsdl⽣成代码,以及Spring-WS如何处理SOAP 请求
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论