উদাহরণ সহ প্রথম SOAP Web Service প্রোগ্রাম

Spring Web Services সেটআপ এবং কনফিগারেশন - স্প্রিং ওয়েব সার্ভিসেস (Spring Web Services) - Java Technologies

251

আমরা এখানে একটি SOAP ওয়েব সার্ভিস তৈরি করব, যা একটি নির্দিষ্ট নাম গ্রহণ করবে এবং একটি স্বাগত বার্তা ফিরিয়ে দেবে।


প্রয়োজনীয় পরিবেশ

  • Java Development Kit (JDK)
  • Spring Boot
  • Maven/Gradle
  • IDE (যেমন IntelliJ IDEA, Eclipse)

প্রোজেক্টের স্ট্রাকচার

src
├── main
│   ├── java
│   │   └── com.example.soapws
│   │       ├── SoapWebServiceApplication.java
│   │       ├── ws
│   │       │   ├── endpoint
│   │       │   │   └── HelloWorldEndpoint.java
│   │       │   ├── config
│   │       │   │   └── WebServiceConfig.java
│   │       │   └── schema
│   │       │       └── HelloWorld.xsd
│   ├── resources
│       ├── application.properties
│       └── wsdl
│           └── HelloWorld.wsdl

ধাপ ১: Maven ডিপেন্ডেন্সি

pom.xml ফাইলে নিচের ডিপেন্ডেন্সি যোগ করুন:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

ধাপ ২: XSD (XML Schema) তৈরি

HelloWorld.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           targetNamespace="http://example.com/helloworld" 
           xmlns="http://example.com/helloworld" 
           elementFormDefault="qualified">
    <xs:element name="HelloRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="HelloResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="message" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

ধাপ ৩: কনফিগারেশন ক্লাস

WebServiceConfig.java:

package com.example.soapws.ws.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.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@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(name = "helloworld")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema helloWorldSchema) {
        DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
        definition.setPortTypeName("HelloWorldPort");
        definition.setLocationUri("/ws");
        definition.setTargetNamespace("http://example.com/helloworld");
        definition.setSchema(helloWorldSchema);
        return definition;
    }

    @Bean
    public XsdSchema helloWorldSchema() {
        return new SimpleXsdSchema(new ClassPathResource("ws/schema/HelloWorld.xsd"));
    }
}

ধাপ ৪: এন্ডপয়েন্ট তৈরি

HelloWorldEndpoint.java:

package com.example.soapws.ws.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.soapws.ws.schema.HelloRequest;
import com.example.soapws.ws.schema.HelloResponse;

@Endpoint
public class HelloWorldEndpoint {
    private static final String NAMESPACE_URI = "http://example.com/helloworld";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "HelloRequest")
    @ResponsePayload
    public HelloResponse sayHello(@RequestPayload HelloRequest request) {
        HelloResponse response = new HelloResponse();
        response.setMessage("Hello, " + request.getName() + "!");
        return response;
    }
}

ধাপ ৫: অ্যাপ্লিকেশন ক্লাস

SoapWebServiceApplication.java:

package com.example.soapws;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SoapWebServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SoapWebServiceApplication.class, args);
    }
}

ধাপ ৬: অ্যাপ্লিকেশন প্রোপার্টিজ

application.properties:

server.port=8080

ধাপ ৭: WSDL ফাইল অ্যাক্সেস করা

আপনার ওয়েব সার্ভার চালু হলে, ব্রাউজারে যান এবং WSDL ফাইল দেখতে নিচের URL ব্যবহার করুন:

http://localhost:8080/ws/helloworld.wsdl

এই উদাহরণটি আপনার প্রথম SOAP ওয়েব সার্ভিস তৈরির জন্য প্রয়োজনীয় ধাপগুলো কভার করে। এটি একটি নাম গ্রহণ করবে এবং সংশ্লিষ্ট স্বাগত বার্তা ফিরিয়ে দেবে।

Content added By
Promotion

Are you sure to start over?

Loading...