স্প্রিং ওএক্সএম (Spring OXM) ব্যবহার করে Custom Exception তৈরি এবং হ্যান্ডল করার জন্য আপনাকে নিচের ধাপগুলো অনুসরণ করতে হবে:
১. Custom Exception তৈরি করা
Custom Exception একটি ক্লাস হিসেবে তৈরি করতে হয়, যা RuntimeException বা Exception থেকে ইনহেরিট করে।
public class XmlProcessingException extends RuntimeException {
public XmlProcessingException(String message) {
super(message);
}
public XmlProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
২. Marshalling/Unmarshalling প্রক্রিয়ায় Exception হ্যান্ডল করা
Spring OXM ব্যবহার করে XML প্রোসেসিংয়ের সময় কোনো সমস্যা হলে Custom Exception তৈরি এবং হ্যান্ডল করা যায়।
উদাহরণ:
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.oxm.XmlMappingException;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.StringWriter;
public class XmlProcessor {
private Jaxb2Marshaller marshaller;
public XmlProcessor() {
marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Employee.class); // Employee ক্লাস সেট করা
}
// Object থেকে XML (Marshalling)
public String marshal(Employee employee) {
try {
StringWriter writer = new StringWriter();
marshaller.marshal(employee, new javax.xml.transform.stream.StreamResult(writer));
return writer.toString();
} catch (XmlMappingException | IllegalArgumentException e) {
throw new XmlProcessingException("Error during marshalling: " + e.getMessage(), e);
}
}
// XML থেকে Object (Unmarshalling)
public Employee unmarshal(File xmlFile) {
try {
return (Employee) marshaller.unmarshal(new StreamSource(xmlFile));
} catch (XmlMappingException | IllegalArgumentException e) {
throw new XmlProcessingException("Error during unmarshalling: " + e.getMessage(), e);
}
}
}
৩. Spring Context Configuration
Spring XML বা Java Config-এ Marshaller কনফিগার করা হয়।
XML Config:
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.example.Employee</value>
</list>
</property>
</bean>
<bean id="xmlProcessor" class="com.example.XmlProcessor">
<property name="marshaller" ref="jaxbMarshaller"/>
</bean>
Java Config:
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 jaxbMarshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Employee.class);
return marshaller;
}
@Bean
public XmlProcessor xmlProcessor() {
XmlProcessor processor = new XmlProcessor();
processor.setMarshaller(jaxbMarshaller());
return processor;
}
}
৪. Custom Exception হ্যান্ডলিং লজিক
Custom Exception-কে লজিক্যালি হ্যান্ডল করার জন্য Spring-এর সাধারণ Exception Handling মেকানিজম ব্যবহার করা হয়।
উদাহরণ:
public class Main {
public static void main(String[] args) {
XmlProcessor processor = new XmlProcessor();
try {
// XML থেকে Object
Employee employee = processor.unmarshal(new File("invalid.xml"));
System.out.println("Employee Name: " + employee.getName());
} catch (XmlProcessingException e) {
System.err.println("Custom Exception Caught: " + e.getMessage());
e.printStackTrace();
}
}
}
৫. Spring-এর @ControllerAdvice ব্যবহার করে Exception হ্যান্ডল করা (Web Application ক্ষেত্রে)
Custom Exception Handler:
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(XmlProcessingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleXmlProcessingException(XmlProcessingException ex) {
// লগ করা বা কাস্টম রেসপন্স প্রদান করা
return "Error: " + ex.getMessage();
}
}
৬. Spring Boot Application-এ Custom Exception হ্যান্ডল করা
Spring Boot-এ @RestControllerAdvice ব্যবহার করা যায়:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class RestGlobalExceptionHandler {
@ExceptionHandler(XmlProcessingException.class)
public ResponseEntity<String> handleXmlProcessingException(XmlProcessingException ex) {
return new ResponseEntity<>("Custom Error: " + ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}
সংক্ষেপে:
- Custom Exception তৈরি করুন।
- XML প্রসেসিংয়ের সময় সেগুলো থ্রো করুন।
- Spring Framework-এর সুবিধা নিয়ে Exception-গুলো গ্রেসফুলি হ্যান্ডল করুন।
এভাবে, স্প্রিং ওএক্সএম ব্যবহার করে যেকোনো XML প্রোসেসিংয়ের সমস্যা আরও পেশাদার উপায়ে হ্যান্ডল করা যায়।