SOAP Fault হল একটি স্ট্যান্ডার্ড প্রক্রিয়া যা SOAP মেসেজের মাধ্যমে ত্রুটি বা সমস্যা সম্পর্কে ক্লায়েন্টকে অবহিত করে। Spring Web Services এই ফিচারটি সমর্থন করে, যা ত্রুটির সময় ডেভেলপারদের আরও কার্যকর উপায়ে সমস্যা হ্যান্ডল করতে সাহায্য করে।
SOAP Fault দুটি প্রধান উপাদানে বিভক্ত:
- Fault Code: ত্রুটির ধরন ব্যাখ্যা করে।
- Fault String: ত্রুটির বিবরণ প্রদান করে।
SOAP Fault তৈরি করার পদ্ধতি
Spring Web Services-এ SOAP Fault তৈরি করতে SoapFault অবজেক্ট ব্যবহার করা হয়।
উদাহরণ: SOAP Fault তৈরি করা
Spring WS-এ ত্রুটি তৈরি করার জন্য একটি Endpoint-এ ত্রুটি হ্যান্ডলিং কোড যোগ করতে হবে।
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 org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapFaultException;
@Endpoint
public class MyServiceEndpoint {
private static final String NAMESPACE_URI = "http://example.com/schemas";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "MyRequest")
@ResponsePayload
public MyResponse handleRequest(@RequestPayload MyRequest request) {
if (request.getId() == null) {
throw new SoapFaultException("ID cannot be null");
}
MyResponse response = new MyResponse();
response.setMessage("Request processed successfully");
return response;
}
}
SOAP Fault Exception Resolver কনফিগার করা
Spring WS-এ ত্রুটি হ্যান্ডল করার জন্য SoapFaultMappingExceptionResolver ব্যবহার করা যায়। এটি কাস্টম ত্রুটি বার্তা তৈরি করে।
Configuration Class:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver;
import org.springframework.ws.soap.SoapFault;
@Configuration
public class SoapFaultConfig {
@Bean
public SoapFaultMappingExceptionResolver exceptionResolver() {
SoapFaultMappingExceptionResolver resolver = new SoapFaultMappingExceptionResolver();
resolver.setDefaultFault(() -> {
SoapFault fault = new SoapFault("Server error", Locale.ENGLISH);
fault.addFaultDetail().addFaultEntry("ErrorDetails", "Details about the error");
return fault;
});
return resolver;
}
}
কাস্টম SOAP Fault তৈরি করা
কাস্টম SOAP Fault তৈরি করতে Spring WS-এর SoapFault ইন্টারফেস ব্যবহার করতে হবে।
Example:
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapMessage;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
public class SoapFaultHelper {
public static String createSoapFault(String faultCode, String faultMessage) {
StringWriter writer = new StringWriter();
SoapFault soapFault = new SoapFault(faultCode, faultMessage, Locale.ENGLISH);
soapFault.addFaultDetail().addFaultEntry("Details", "Invalid input provided");
soapFault.writeTo(new StreamResult(writer));
return writer.toString();
}
}
ত্রুটি হ্যান্ডলিং
Spring Web Services ত্রুটি হ্যান্ডল করার জন্য @ExceptionHandler বা SOAP-specific Exception Resolver ব্যবহার করতে পারে।
Exception Handler Example:
import org.springframework.ws.soap.server.endpoint.annotation.FaultCode;
import org.springframework.ws.soap.server.endpoint.annotation.SoapFault;
@SoapFault(faultCode = FaultCode.CLIENT)
public class InvalidRequestException extends RuntimeException {
public InvalidRequestException(String message) {
super(message);
}
}
Endpoint with Exception Handling:
@Endpoint
public class MyEndpoint {
@PayloadRoot(namespace = "http://example.com/schema", localPart = "Request")
public Response handleRequest(Request request) {
if (request.getValue() == null) {
throw new InvalidRequestException("Value cannot be null");
}
return new Response("Success");
}
}
SOAP Fault এর উদাহরণ (XML ফরম্যাট)
SOAP Fault ক্লায়েন্টের কাছে নিম্নলিখিত XML ফরম্যাটে পাঠানো হয়:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring>Invalid input provided</faultstring>
<detail>
<ErrorDetails>
Details about the error
</ErrorDetails>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
সারাংশ
SOAP Fault Spring Web Services-এ ত্রুটি হ্যান্ডল করার একটি স্ট্যান্ডার্ড উপায়। এটি ত্রুটি সংক্রান্ত তথ্য ক্লায়েন্টকে পাঠানোর পাশাপাশি সার্ভার-সাইড সমস্যা শনাক্ত করতেও সহায়তা করে। Spring WS ত্রুটি হ্যান্ডলিং এবং কাস্টমাইজেশনের জন্য উন্নত ফিচার সরবরাহ করে।
Read more