My linux world » Java Spring – SOAP

Java Spring - SOAP


Contents

Prerequistes

Please read : Java spring – quickstart

Configuration

Create xsd file

Create the file /src/main/resources/students.xsd :

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://rabahi.net/soap"
           targetNamespace="http://rabahi.net/soap" elementFormDefault="qualified">
 
    <xsd:element name="getStudentRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="studentId" type="xsd:int"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="getStudentResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="student" type="student"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="student">
        <xsd:sequence>
            <xsd:element name="studentId" type="xsd:int"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="age" type="xsd:int"/>
            <xsd:element name="class" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
 
</xsd:schema>

Spring configuration

Create the WebappInitializer.java :

public class WebAppInitializer implements WebApplicationInitializer {
 
	/* (non-Javadoc)
	 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
	 */
	public void onStartup(ServletContext servletContext) throws ServletException {  
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
        ctx.register(AppConfig.class);  
        ctx.setServletContext(servletContext);    
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
		servlet.setApplicationContext(ctx);
		servlet.setTransformWsdlLocations(true);
        Dynamic dynamic = servletContext.addServlet("dispatcher",servlet);  
        dynamic.addMapping("/soapws/*");  
        dynamic.setLoadOnStartup(1);  
   }  
}

And Appconfig.java :

@Configuration
@EnableWs
@ComponentScan("net.rabahi.java.jee.spring") 
public class AppConfig extends WsConfigurerAdapter {
 
	@Bean(name = "students")
	public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema studentsSchema) {
		DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
		wsdl11Definition.setPortTypeName("StudentsPort");
		wsdl11Definition.setLocationUri("/soapws");
		wsdl11Definition.setTargetNamespace("http://rabahi.net/soap");
		wsdl11Definition.setSchema(studentsSchema);
		return wsdl11Definition;
	}
 
	/**
	 * @return SimpleXsdSchema
	 */
	@Bean
	public XsdSchema studentsSchema() {
		return new SimpleXsdSchema(new ClassPathResource("students.xsd"));
	}
}

wsdl

The wsdl is available here : http://localhost:8080/soapws/students.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://rabahi.net/soap" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://rabahi.net/soap" targetNamespace="http://rabahi.net/soap">
  <wsdl:types>
    <xsd:schema xmlns="http://rabahi.net/soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://rabahi.net/soap">
 
    <xsd:element name="getStudentRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="studentId" type="xsd:int"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="getStudentResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="student" type="student"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="student">
        <xsd:sequence>
            <xsd:element name="studentId" type="xsd:int"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="age" type="xsd:int"/>
            <xsd:element name="class" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
 
</xsd:schema>
  </wsdl:types>
  <wsdl:message name="getStudentRequest">
    <wsdl:part element="tns:getStudentRequest" name="getStudentRequest">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getStudentResponse">
    <wsdl:part element="tns:getStudentResponse" name="getStudentResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="StudentsPort">
    <wsdl:operation name="getStudent">
      <wsdl:input message="tns:getStudentRequest" name="getStudentRequest">
    </wsdl:input>
      <wsdl:output message="tns:getStudentResponse" name="getStudentResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="StudentsPortSoap11" type="tns:StudentsPort">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getStudent">
      <soap:operation soapAction=""/>
      <wsdl:input name="getStudentRequest">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getStudentResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="StudentsPortService">
    <wsdl:port binding="tns:StudentsPortSoap11" name="StudentsPortSoap11">
      <soap:address location="http://localhost:1234/soapws"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

And you can request it with your favorite soap client.
Note: you can download soapui client.


Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.