Spring Boot SOAP Webservice例子 (2)

出于简便考虑,我们将直接在代码里初始化相关数据,代码如下:
创建StudentRepository.java,加上@Repository注解,添加findStudent()方法:

package com.example.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 com.howtodoinjava.xml.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"); student.setStandard(5); student.setAddress("Pune"); students.put(student.getName(), student); student = new Student(); student.setName("Kajal"); student.setStandard(5); student.setAddress("Chicago"); students.put(student.getName(), student); student = new Student(); student.setName("Lokesh"); student.setStandard(6); student.setAddress("Delhi"); students.put(student.getName(), student); student = new Student(); student.setName("Sukesh"); student.setStandard(7); student.setAddress("Noida"); students.put(student.getName(), student); } public Student findStudent(String name) { Assert.notNull(name, "The Student\'s name must not be null"); return students.get(name); } } 添加SOAP Webservice 配置

添加一个带 @Configuration注解的配置类:

package com.example.howtodoinjava.springbootsoapservice; 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 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("http://www.howtodoinjava.com/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 暴露出去的名称,我们可以通过:8080/service/studentDetailsWsdl.wsdl路径访问;

Spring boot SOAP webservice例子演示

使用mvn clean install maven命名构建工程,并使用java -jar target\spring-boot-soap-service-0.0.1-SNAPSHOT.jar命令启动应用;

执行完以上操作后,将会以默认的8080端口启动一个tomcat服务,本应用将部署在该服务里;

现在我们可以访问:8080/service/studentDetailsWsdl.wsdl路径,确认wsdl是否是正确的:

file

如果我们的wsdl没问题的话,我们可以使用该WSDL 在SOAP ui 里创建一个工程,并测试该应用,请求和响应示例如下:
请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://www.howtodoinjava.com/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="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <ns2:StudentDetailsResponse xmlns:ns2="http://www.howtodoinjava.com/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>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggsjg.html