উদাহরণ সহ Marshalling এবং Unmarshalling

Marshalling এবং Unmarshalling - স্প্রিং ওয়েব সার্ভিসেস (Spring Web Services) - Java Technologies

357

ভূমিকা

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-এর একটি গুরুত্বপূর্ণ অংশ।

Content added By
Promotion

Are you sure to start over?

Loading...