Spring Web Services-এ Marshalling এবং Unmarshalling হল এমন প্রক্রিয়া, যা XML এবং Java Object-এর মধ্যে রূপান্তর ঘটায়। এটি SOAP মেসেজ প্রসেসিংয়ের গুরুত্বপূর্ণ অংশ, কারণ মেসেজ এক্সচেঞ্জের সময় ডেটা XML আকারে থাকে এবং এটি Java Object-এ রূপান্তর করার প্রয়োজন হয়।
Marshalling এবং Unmarshalling কী?
Marshalling
Marshalling হল প্রক্রিয়া, যা Java Object-কে XML ফর্ম্যাটে রূপান্তর করে। এটি ক্লায়েন্ট থেকে সার্ভারে ডেটা পাঠানোর সময় ব্যবহৃত হয়।
Unmarshalling
Unmarshalling হল XML ডেটাকে Java Object-এ রূপান্তর করার প্রক্রিয়া। এটি সার্ভার থেকে ক্লায়েন্টে ডেটা রিসিভ করার সময় ব্যবহৃত হয়।
স্প্রিং ওয়েব সার্ভিসে ব্যবহৃত Marshalling Framework
Spring Web Services বিভিন্ন Marshalling Framework সমর্থন করে, যেমন:
- JAXB (Java Architecture for XML Binding): সবচেয়ে বেশি ব্যবহৃত এবং কার্যকর ফ্রেমওয়ার্ক।
- Castor: XML এবং Java Object-এর মধ্যে রূপান্তরের জন্য।
- XStream: Object-to-XML রূপান্তরের জন্য একটি সহজ এবং দ্রুত পদ্ধতি।
- XmlBeans: Apache কর্তৃক প্রস্তাবিত একটি টুল।
JAXB ব্যবহার করে Marshalling এবং Unmarshalling
Maven ডিপেন্ডেন্সি
JAXB ব্যবহার করার জন্য Maven-এ ডিপেন্ডেন্সি যুক্ত করুন।
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
JAXB-এনোটেটেড ক্লাস তৈরি
JAXB-এনোটেটেড ক্লাস তৈরি করুন, যা XML ডেটার কাঠামো নির্দেশ করে।
@XmlRootElement(namespace = "http://example.com/ws")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetExampleRequest {
@XmlElement(name = "id")
private int id;
// Getter এবং Setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@XmlRootElement(namespace = "http://example.com/ws")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetExampleResponse {
@XmlElement(name = "message")
private String message;
// Getter এবং Setter
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Spring Web Services-এ JAXB Marshalling কনফিগারেশন
SOAP মেসেজের জন্য JAXB Marshalling এবং Unmarshalling কনফিগার করতে নিম্নলিখিত পদ্ধতি অনুসরণ করুন:
Marshalling Bean তৈরি
@Configuration
@EnableWs
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.example.ws"); // JAXB-এনোটেটেড ক্লাসের প্যাকেজ
return marshaller;
}
@Bean
public ExampleClient exampleClient(Jaxb2Marshaller marshaller) {
ExampleClient client = new ExampleClient();
client.setDefaultUri("http://localhost:8080/ws");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
এন্ডপয়েন্টে Marshalling এবং Unmarshalling প্রয়োগ
Spring Web Services এন্ডপয়েন্ট তৈরি করার সময় @RequestPayload এবং @ResponsePayload annotations ব্যবহার করে ডেটা রূপান্তর করুন।
@Endpoint
public class ExampleEndpoint {
@PayloadRoot(namespace = "http://example.com/ws", localPart = "GetExampleRequest")
@ResponsePayload
public GetExampleResponse handleRequest(@RequestPayload GetExampleRequest request) {
GetExampleResponse response = new GetExampleResponse();
response.setMessage("Hello, your ID is " + request.getId());
return response;
}
}
Marshalling এবং Unmarshalling এর সুবিধা
- ডেটা রূপান্তর সহজতর করে: XML এবং Java Object-এর মধ্যে রূপান্তর স্বয়ংক্রিয় হয়।
- ডেটার বৈধতা নিশ্চিত করে: XML স্কিমা অনুযায়ী ডেটা যাচাই করা যায়।
- কম কোডিং: Spring Framework-এর মাধ্যমে কম কোড লিখেই এই প্রক্রিয়া সম্পন্ন করা যায়।
- পোর্টেবল: JAXB বা অন্যান্য ফ্রেমওয়ার্ক ব্যবহার করলে কোড সহজে অন্যান্য প্রজেক্টে পুনরায় ব্যবহার করা যায়।
সারাংশ
Marshalling এবং Unmarshalling Spring Web Services-এর একটি অপরিহার্য অংশ, যা XML এবং Java Object-এর মধ্যে ডেটা রূপান্তর করে। JAXB বা অন্যান্য Marshalling Framework ব্যবহার করে এই প্রক্রিয়া দ্রুত এবং কার্যকর করা যায়। এটি ডেভেলপারদের SOAP মেসেজ হ্যান্ডলিং সহজ করতে সহায়তা করে।
Marshalling এবং Unmarshalling কি?
Marshalling এবং Unmarshalling হল XML এবং জাভা অবজেক্টের মধ্যে ডেটা রূপান্তরের দুটি প্রক্রিয়া। স্প্রিং ওয়েব সার্ভিসে এগুলো XML বার্তা প্রক্রিয়াকরণের গুরুত্বপূর্ণ অংশ।
- Marshalling: একটি জাভা অবজেক্টকে XML ফরম্যাটে রূপান্তর করার প্রক্রিয়া। এটি ওয়েব সার্ভিসে ডেটা পাঠানোর জন্য ব্যবহৃত হয়।
- Unmarshalling: XML ডেটাকে একটি জাভা অবজেক্টে রূপান্তর করার প্রক্রিয়া। এটি ওয়েব সার্ভিস থেকে ডেটা গ্রহণ করার সময় ব্যবহৃত হয়।
Marshalling এবং Unmarshalling এর ভূমিকা
ওয়েব সার্ভিসে XML ফরম্যাট সবচেয়ে বেশি ব্যবহৃত হয় কারণ এটি স্ট্রাকচার্ড এবং বিভিন্ন প্ল্যাটফর্মে সমর্থনযোগ্য। স্প্রিং ওয়েব সার্ভিসে Marshalling এবং Unmarshalling নিম্নলিখিত ক্ষেত্রে ব্যবহৃত হয়:
- SOAP বার্তা প্রক্রিয়াকরণ: সার্ভিসের ইনপুট বা আউটপুট ডেটাকে জাভা অবজেক্ট থেকে XML-এ এবং XML থেকে জাভা অবজেক্টে রূপান্তর।
- ডেটা ভ্যালিডেশন: XML স্কিমা (XSD) ব্যবহার করে ইনপুট বা আউটপুট ডেটা যাচাই করা।
- পোর্টেবল ডেটা ফরম্যাট: জাভা অবজেক্টকে XML-এ রূপান্তর করে এটি সহজেই অন্যান্য সিস্টেম বা অ্যাপ্লিকেশনের সঙ্গে শেয়ার করা যায়।
স্প্রিং-এ Marshalling এবং Unmarshalling এর প্রক্রিয়া
স্প্রিং ওয়েব সার্ভিসে Marshalling এবং Unmarshalling করার জন্য বিভিন্ন ফ্রেমওয়ার্ক এবং টুল ব্যবহার করা যায়, যেমন:
- JAXB (Java Architecture for XML Binding)
- Castor
- XStream
- XMLBeans
JAXB উদাহরণ:
১. JAXB অ্যাটোটেশন সহ মডেল ক্লাস তৈরি করা:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Employee")
public class Employee {
private int id;
private String name;
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
২. Marshalling (Java Object to XML):
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class MarshallingExample {
public static void main(String[] args) throws JAXBException {
Employee employee = new Employee();
employee.setId(101);
employee.setName("John Doe");
JAXBContext context = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, System.out);
}
}
আউটপুট:
<Employee>
<id>101</id>
<name>John Doe</name>
</Employee>
৩. Unmarshalling (XML to Java Object):
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
public class UnmarshallingExample {
public static void main(String[] args) throws JAXBException {
String xml = "<Employee><id>101</id><name>John Doe</name></Employee>";
JAXBContext context = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Employee employee = (Employee) unmarshaller.unmarshal(new StringReader(xml));
System.out.println("Employee ID: " + employee.getId());
System.out.println("Employee Name: " + employee.getName());
}
}
স্প্রিং ওয়েব সার্ভিসে Marshalling এবং Unmarshalling এর ইমপ্লিমেন্টেশন
স্প্রিং ওয়েব সার্ভিসে Marshalling এবং Unmarshalling করার জন্য Marshaller এবং Unmarshaller ইন্টারফেস ব্যবহার করা হয়।
উদাহরণ:
কনফিগারেশন:
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("com.example.model");
return marshaller;
}
}
SOAP এন্ডপয়েন্টে ব্যবহার:
@Endpoint
public class EmployeeEndpoint {
@PayloadRoot(namespace = "http://example.com/namespace", localPart = "EmployeeRequest")
public EmployeeResponse handleEmployeeRequest(EmployeeRequest request) {
EmployeeResponse response = new EmployeeResponse();
response.setId(request.getId());
response.setName("Processed: " + request.getName());
return response;
}
}
সুবিধা
- সহজ রূপান্তর: জাভা অবজেক্ট এবং XML ফরম্যাটের মধ্যে রূপান্তর সহজতর হয়।
- কোড রিডেবিলিটি: JAXB অ্যাটোটেশন ব্যবহার করলে কোড পড়া ও বুঝা সহজ হয়।
- স্কিমা সমর্থন: XML স্কিমা (XSD) ফাইল ব্যবহার করে ডেটা যাচাই করা যায়।
সারাংশ:
Marshalling এবং Unmarshalling হল স্প্রিং ওয়েব সার্ভিসে XML এবং জাভা অবজেক্টের মধ্যে রূপান্তরের দুটি গুরুত্বপূর্ণ প্রক্রিয়া। JAXB, Spring Bean কনফিগারেশন এবং SOAP এন্ডপয়েন্টের মাধ্যমে এগুলো কার্যকরভাবে বাস্তবায়ন করা যায়। এটি ওয়েব সার্ভিসের ডেটা প্রক্রিয়াকরণকে সহজ এবং সঠিক করে তোলে।
Spring OXM (Object XML Mapping) একটি Spring ফ্রেমওয়ার্কের অংশ, যা জাভা অবজেক্ট এবং XML ডকুমেন্টের মধ্যে ম্যাপিং (Mapping) সহজ করে। এটি JAXB, Castor, XStream, এবং JIBX-এর মতো বিভিন্ন XML মার্শালিং (Marshalling) এবং আনমার্শালিং (Unmarshalling) ফ্রেমওয়ার্ক সমর্থন করে।
Spring OXM সাধারণত SOAP মেসেজ তৈরি এবং প্রসেস করার সময় ব্যবহৃত হয়, বিশেষ করে Spring Web Services এ। নিচে Spring OXM এর ব্যবহারের জন্য একটি গাইড দেওয়া হলো।
Spring OXM এর মূল বৈশিষ্ট্য
- Marshalling (মার্শালিং): জাভা অবজেক্টকে XML-এ রূপান্তর করা।
- Unmarshalling (আনমার্শালিং): XML-কে জাভা অবজেক্টে রূপান্তর করা।
- Abstract API: বিভিন্ন XML-ম্যাপিং টুলের জন্য সমর্থন।
- Integration: Spring Web Services এর সাথে সহজ ইন্টিগ্রেশন।
Spring OXM ব্যবহার করার ধাপ
১. প্রয়োজনীয় ডিপেনডেন্সি যুক্ত করা
Spring OXM এবং JAXB এর জন্য Maven অথবা Gradle ডিপেনডেন্সি যুক্ত করুন।
Maven:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.3.12</version> <!-- সর্বশেষ ভার্সন ব্যবহার করুন -->
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.1</version>
</dependency>
Gradle:
implementation 'org.springframework:spring-oxm:5.3.12'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.1'
implementation 'org.glassfish.jaxb:jaxb-runtime:3.0.1'
২. JAXB মার্শালার কনফিগারেশন
JAXB ব্যবহার করে XML এবং জাভা অবজেক্টের মধ্যে ম্যাপিং করার জন্য একটি Jaxb2Marshaller বীন তৈরি করতে হবে।
Configuration Class:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class AppConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// আপনার JAXB কন্টেক্সট প্যাকেজটি এখানে নির্ধারণ করুন
marshaller.setContextPath("com.example.model");
return marshaller;
}
}
৩. জাভা ক্লাস তৈরি করা (XML এর জন্য)
JAXB এনোটেশন ব্যবহার করে XML এর জন্য মডেল ক্লাস তৈরি করুন।
Example:
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Person")
public class Person {
private String name;
private int age;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
৪. মার্শালিং এবং আনমার্শালিং ব্যবহার
Spring OXM এর সাহায্যে জাভা অবজেক্ট এবং XML এর মধ্যে রূপান্তর করুন।
Marshalling (Object to XML):
import org.springframework.oxm.Marshaller;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
public class MarshallingExample {
private final Marshaller marshaller;
public MarshallingExample(Marshaller marshaller) {
this.marshaller = marshaller;
}
public String convertObjectToXml(Person person) throws Exception {
StringWriter writer = new StringWriter();
marshaller.marshal(person, new StreamResult(writer));
return writer.toString();
}
}
Unmarshalling (XML to Object):
import org.springframework.oxm.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
public class UnmarshallingExample {
private final Unmarshaller unmarshaller;
public UnmarshallingExample(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public Person convertXmlToObject(String xml) throws Exception {
StringReader reader = new StringReader(xml);
return (Person) unmarshaller.unmarshal(new StreamSource(reader));
}
}
Spring OXM এর সুবিধা
- জাভা অবজেক্ট এবং XML এর মধ্যে সহজ রূপান্তর।
- বিভিন্ন XML-ম্যাপিং ফ্রেমওয়ার্ক সমর্থন।
- Spring Framework এর সাথে সহজ ইন্টিগ্রেশন।
- SOAP মেসেজ প্রসেসিং সহজ করে।
Spring OXM ব্যবহার করে আপনি Spring Web Services এর জন্য XML ম্যাপিং আরও কার্যকরভাবে পরিচালনা করতে পারবেন।
Spring Web Services (Spring WS) বিভিন্ন ডাটা বাইন্ডিং ফ্রেমওয়ার্ক যেমন JAXB, Castor, এবং XStream এর মাধ্যমে XML এবং জাভা অবজেক্টের মধ্যে ডাটা কনভার্সন সহজ করে। এই ইন্টিগ্রেশন ডেভেলপারদের SOAP সার্ভিসে ডাটা প্রক্রিয়াকরণ আরও কার্যকর করতে সাহায্য করে।
JAXB (Java Architecture for XML Binding)
JAXB কি?
JAXB হল জাভা অবজেক্ট এবং XML ডকুমেন্টের মধ্যে ডাটা কনভার্ট করার একটি ফ্রেমওয়ার্ক। এটি অ্যানোটেশন-ভিত্তিক এবং Spring Web Services এ XML Marshalling/Unmarshalling এর জন্য বহুল ব্যবহৃত।
Spring Web Services এ JAXB ইন্টিগ্রেশন
ডিপেনডেন্সি (Maven):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
JAXB কনফিগারেশন:
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.example.demo.model");
return marshaller;
}
JAXB ব্যবহার:
@Bean
public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
WebServiceTemplate template = new WebServiceTemplate();
template.setMarshaller(marshaller);
template.setUnmarshaller(marshaller);
return template;
}
Castor
Castor কি?
Castor একটি ডাটা বাইন্ডিং ফ্রেমওয়ার্ক যা জাভা অবজেক্ট এবং XML এর মধ্যে ডাটা কনভার্ট করে। এটি জাভা এবং ডাটাবেসের মধ্যে ডাটা ম্যাপিংয়ের জন্যও পরিচিত।
Spring Web Services এ Castor ইন্টিগ্রেশন
ডিপেনডেন্সি (Maven):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>1.4.1</version>
</dependency>
Castor কনফিগারেশন:
@Bean
public CastorMarshaller marshaller() {
CastorMarshaller marshaller = new CastorMarshaller();
marshaller.setMappingLocation(new ClassPathResource("castor-mapping.xml"));
return marshaller;
}
Castor ব্যবহার:
@Bean
public WebServiceTemplate webServiceTemplate(CastorMarshaller marshaller) {
WebServiceTemplate template = new WebServiceTemplate();
template.setMarshaller(marshaller);
template.setUnmarshaller(marshaller);
return template;
}
Castor Mapping ফাইল (castor-mapping.xml):
<mapping>
<class name="com.example.demo.model.User">
<map-to xml="User" />
<field name="name" type="string" />
<field name="email" type="string" />
</class>
</mapping>
XStream
XStream কি?
XStream হল একটি সহজ এবং হালকা ডাটা বাইন্ডিং ফ্রেমওয়ার্ক, যা XML এবং জাভা অবজেক্টের মধ্যে ডাটা কনভার্ট করে। এটি ব্যবহার করা খুব সহজ এবং অ্যানোটেশন ছাড়াই কাজ করে।
Spring Web Services এ XStream ইন্টিগ্রেশন
ডিপেনডেন্সি (Maven):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.20</version>
</dependency>
XStream কনফিগারেশন:
@Bean
public XStreamMarshaller marshaller() {
XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.setAnnotatedClasses(User.class);
return marshaller;
}
XStream ব্যবহার:
@Bean
public WebServiceTemplate webServiceTemplate(XStreamMarshaller marshaller) {
WebServiceTemplate template = new WebServiceTemplate();
template.setMarshaller(marshaller);
template.setUnmarshaller(marshaller);
return template;
}
মডেল ক্লাস উদাহরণ:
@XStreamAlias("User")
public class User {
private String name;
private String email;
// Getters and Setters
}
কাকে নির্বাচন করবেন?
- JAXB: যদি XML Schema (XSD) ব্যবহার করে কনভার্সন করতে চান এবং অ্যানোটেশন ভিত্তিক ডেভেলপমেন্ট পছন্দ করেন।
- Castor: জাভা অবজেক্ট থেকে XML ডাটা ম্যাপিং কাস্টমাইজ করতে হলে।
- XStream: সহজ এবং দ্রুত কনভার্সন প্রয়োজন হলে, যেখানে অ্যানোটেশন ব্যবহার বাধ্যতামূলক নয়।
সারাংশ
Spring Web Services এ JAXB, Castor, এবং XStream এর সাথে ইন্টিগ্রেশন ডাটা প্রক্রিয়াকরণকে আরও সহজ, দ্রুত এবং কার্যকর করে। আপনার প্রয়োজন এবং প্রোজেক্টের জটিলতার উপর ভিত্তি করে সঠিক ফ্রেমওয়ার্ক নির্বাচন করতে হবে।
ভূমিকা
Marshalling এবং Unmarshalling হল দুটি গুরুত্বপূর্ণ প্রক্রিয়া যা ডেটাকে XML বা JSON ফরম্যাটে রূপান্তর এবং পুনরায় অবজেক্টে রূপান্তর করার জন্য ব্যবহৃত হয়।
- Marshalling: একটি জাভা অবজেক্টকে XML বা JSON এ রূপান্তর করা।
- Unmarshalling: একটি XML বা JSON ডেটাকে জাভা অবজেক্টে রূপান্তর করা।
স্প্রিং ওয়েব সার্ভিসে সাধারণত এই কাজের জন্য JAXB (Java Architecture for XML Binding) বা অন্যান্য টুল ব্যবহার করা হয়।
উদাহরণ: JAXB ব্যবহার করে Marshalling এবং Unmarshalling
আমরা একটি SOAP ওয়েব সার্ভিস তৈরি করব, যেখানে JAXB ব্যবহার করে একটি Request অবজেক্ট XML-এ রূপান্তর করা হবে এবং XML-কে Response অবজেক্টে রূপান্তর করা হবে।
ধাপ ১: প্রোজেক্ট স্ট্রাকচার
src
├── main
│ ├── java
│ │ └── com.example.marshalling
│ │ ├── MarshallingExampleApplication.java
│ │ ├── model
│ │ │ ├── HelloRequest.java
│ │ │ ├── HelloResponse.java
│ │ ├── endpoint
│ │ │ └── HelloWorldEndpoint.java
│ │ ├── config
│ │ │ └── WebServiceConfig.java
│ ├── resources
│ ├── application.properties
│ └── wsdl
│ └── HelloWorld.wsdl
ধাপ ২: JAXB ব্যবহার করে মডেল ক্লাস তৈরি
HelloRequest.java:
package com.example.marshalling.model;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "HelloRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class HelloRequest {
@XmlElement(name = "name")
private String name;
// গেটার এবং সেটার
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HelloResponse.java:
package com.example.marshalling.model;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "HelloResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class HelloResponse {
@XmlElement(name = "message")
private String message;
// গেটার এবং সেটার
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
ধাপ ৩: কনফিগারেশন ক্লাস তৈরি
WebServiceConfig.java:
package com.example.marshalling.config;
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.ws.config.annotation.EnableWs;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@EnableWs
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext context) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(context);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("com.example.marshalling.model");
return marshaller;
}
}
ধাপ ৪: এন্ডপয়েন্ট তৈরি
HelloWorldEndpoint.java:
package com.example.marshalling.endpoint;
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.example.marshalling.model.HelloRequest;
import com.example.marshalling.model.HelloResponse;
@Endpoint
public class HelloWorldEndpoint {
private static final String NAMESPACE_URI = "http://example.com/helloworld";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "HelloRequest")
@ResponsePayload
public HelloResponse handleHelloRequest(@RequestPayload HelloRequest request) {
// Unmarshalling: XML থেকে অবজেক্টে রূপান্তর
String name = request.getName();
// Business Logic
String message = "Hello, " + name + "!";
// Marshalling: অবজেক্ট থেকে XML-এ রূপান্তর
HelloResponse response = new HelloResponse();
response.setMessage(message);
return response;
}
}
ধাপ ৫: অ্যাপ্লিকেশন ক্লাস
MarshallingExampleApplication.java:
package com.example.marshalling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MarshallingExampleApplication {
public static void main(String[] args) {
SpringApplication.run(MarshallingExampleApplication.class, args);
}
}
ধাপ ৬: WSDL তৈরি
HelloWorld.wsdl:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example.com/helloworld"
targetNamespace="http://example.com/helloworld">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/helloworld">
<element name="HelloRequest">
<complexType>
<sequence>
<element name="name" type="string"/>
</sequence>
</complexType>
</element>
<element name="HelloResponse">
<complexType>
<sequence>
<element name="message" type="string"/>
</sequence>
</complexType>
</element>
</schema>
</types>
<message name="HelloRequest">
<part name="parameters" element="tns:HelloRequest"/>
</message>
<message name="HelloResponse">
<part name="parameters" element="tns:HelloResponse"/>
</message>
<portType name="HelloWorldPort">
<operation name="SayHello">
<input message="tns:HelloRequest"/>
<output message="tns:HelloResponse"/>
</operation>
</portType>
<binding name="HelloWorldBinding" type="tns:HelloWorldPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="SayHello">
<soap:operation soapAction="http://example.com/helloworld/SayHello"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWorldService">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost:8080/ws"/>
</port>
</service>
</definitions>
পরীক্ষা
SOAP ক্লায়েন্ট ব্যবহার করে HelloRequest পাঠানোর পর আপনি HelloResponse পাবেন:
SOAP Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hell="http://example.com/helloworld">
<soapenv:Header/>
<soapenv:Body>
<hell:HelloRequest>
<hell:name>John</hell:name>
</hell:HelloRequest>
</soapenv:Body>
</soapenv:Envelope>
SOAP Response:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<HelloResponse xmlns="http://example.com/helloworld">
<message>Hello, John!</message>
</HelloResponse>
</soapenv:Body>
</soapenv:Envelope>
এই উদাহরণে JAXB ব্যবহার করে সহজে Marshalling এবং Unmarshalling দেখানো হয়েছে, যা Spring Web Services-এর একটি গুরুত্বপূর্ণ অংশ।
Read more