স্প্রিং ওয়েব সার্ভিসেস (Spring Web Services) ব্যবহার করে SOAP ভিত্তিক ওয়েব সার্ভিস তৈরি করার প্রক্রিয়া অনেক সহজ। এখানে একটি স্টেপ-বাই-স্টেপ গাইড দেওয়া হলো, যেখানে একটি উদাহরণ হিসেবে একটি সিম্পল SOAP ওয়েব সার্ভিস তৈরি করা হবে যা ব্যবহারকারীর নামের ভিত্তিতে একটি স্বাগত বার্তা প্রদান করবে।
প্রকল্পের প্রাথমিক সেটআপ
Maven ডিপেন্ডেন্সি
প্রথমে, pom.xml ফাইলে প্রয়োজনীয় ডিপেন্ডেন্সি যুক্ত করুন:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
XML স্কিমা তৈরি
SOAP ওয়েব সার্ভিসে ব্যবহার করা হবে এমন ডেটার গঠন (structure) ডিফাইন করার জন্য একটি XML স্কিমা (XSD) তৈরি করুন।
greeting.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/webservice"
xmlns="http://example.com/webservice"
elementFormDefault="qualified">
<xs:element name="GetGreetingRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetGreetingResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
JAXB ক্লাস জেনারেট করা
উপরের স্কিমা থেকে JAXB ক্লাস জেনারেট করতে maven-jaxb2-plugin ব্যবহার করুন:
pom.xml এ JAXB প্লাগইন যুক্ত করুন:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
<outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory>
</configuration>
</plugin>
XSD ফাইলটি src/main/resources/xsd ডিরেক্টরিতে রাখুন এবং Maven Build চালান। JAXB ক্লাসগুলো target/generated-sources/jaxb ফোল্ডারে জেনারেট হবে।
SOAP এন্ডপয়েন্ট তৈরি
SOAP এন্ডপয়েন্ট হ্যান্ডলিংয়ের জন্য একটি ক্লাস তৈরি করুন:
GreetingEndpoint.java
@Endpoint
public class GreetingEndpoint {
private static final String NAMESPACE_URI = "http://example.com/webservice";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetGreetingRequest")
@ResponsePayload
public GetGreetingResponse getGreeting(@RequestPayload GetGreetingRequest request) {
GetGreetingResponse response = new GetGreetingResponse();
response.setMessage("Hello, " + request.getName() + "! Welcome to Spring Web Services.");
return response;
}
}
Spring Bean কনফিগারেশন
SOAP মেসেজ প্রসেস করার জন্য MessageDispatcherServlet কনফিগার করুন:
WebServiceConfig.java
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}
@Bean(name = "greetings")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema greetingSchema) {
DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
definition.setPortTypeName("GreetingPort");
definition.setLocationUri("/ws");
definition.setTargetNamespace("http://example.com/webservice");
definition.setSchema(greetingSchema);
return definition;
}
@Bean
public XsdSchema greetingSchema() {
return new SimpleXsdSchema(new ClassPathResource("xsd/greeting.xsd"));
}
}
অ্যাপ্লিকেশন চালু করা
Spring Boot অ্যাপ্লিকেশন চালু করার জন্য main ক্লাস:
SpringWebServiceApplication.java
@SpringBootApplication
public class SpringWebServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SPRINGWebServiceApplication.class, args);
}
}
WSDL অ্যাক্সেস করা
অ্যাপ্লিকেশন চালু করার পর ব্রাউজারে http://localhost:8080/ws/greetings.wsdl URL-এ গেলে WSDL ফাইল দেখা যাবে। এটি SOAP ক্লায়েন্ট তৈরি করতে ব্যবহার করা যাবে।
SOAP ক্লায়েন্ট দিয়ে টেস্ট
SOAP ক্লায়েন্ট ব্যবহার করে XML মেসেজ পাঠান এবং সার্ভারের উত্তর পান:
SOAP Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://example.com/webservice">
<soapenv:Header/>
<soapenv:Body>
<web:GetGreetingRequest>
<web:name>John</web:name>
</web:GetGreetingRequest>
</soapenv:Body>
</soapenv:Envelope>
SOAP Response:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://example.com/webservice">
<soapenv:Header/>
<soapenv:Body>
<web:GetGreetingResponse>
<web:message>Hello, John! Welcome to Spring Web Services.</web:message>
</web:GetGreetingResponse>
</soapenv:Body>
</soapenv:Envelope>
সারাংশ
এই গাইডের মাধ্যমে একটি SOAP ভিত্তিক ওয়েব সার্ভিস তৈরি করার প্রক্রিয়া ব্যাখ্যা করা হলো। এটি ব্যবহার করে সহজে এবং কার্যকরভাবে Spring Web Services দিয়ে SOAP এন্ডপয়েন্ট তৈরি করা যায়।
Read more