अपवाद प्रबंधन मजबूत और उपयोगकर्ता के अनुकूल अनुप्रयोगों के निर्माण का एक महत्वपूर्ण हिस्सा है। स्प्रिंग बूट में, हम यह सुनिश्चित करने के लिए विभिन्न तरीकों से अपवादों को संभाल सकते हैं कि हमारा एप्लिकेशन स्थिर रहे और उपयोगकर्ताओं को सार्थक प्रतिक्रिया प्रदान करे। यह मार्गदर्शिका अपवाद प्रबंधन के लिए विभिन्न रणनीतियों को कवर करेगी, जिसमें कस्टम अपवाद, वैश्विक अपवाद प्रबंधन, सत्यापन त्रुटियां और उत्पादन के लिए सर्वोत्तम अभ्यास शामिल हैं।
अपवाद ऐसी घटनाएं हैं जो किसी प्रोग्राम के सामान्य प्रवाह को बाधित करती हैं। उन्हें इसमें विभाजित किया जा सकता है:
कस्टम अपवाद कक्षाएं बनाने से आपके एप्लिकेशन में विशिष्ट त्रुटि स्थितियों को संभालने में मदद मिलती है।
package com.example.SpringBootRefresher.exception; public class DepartmentNotFoundException extends RuntimeException { public DepartmentNotFoundException(String message) { super(message); } }
@ExceptionHandler एनोटेशन:
आप अपने नियंत्रक वर्गों में अपवादों को संभालने के तरीकों को परिभाषित कर सकते हैं।
package com.example.SpringBootRefresher.controller; import com.example.SpringBootRefresher.exception.DepartmentNotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DepartmentController { @GetMapping("/department") public String getDepartment() { // Simulate an exception throw new DepartmentNotFoundException("Department not found!"); } @ExceptionHandler(DepartmentNotFoundException.class) public ResponseEntityhandleDepartmentNotFoundException(DepartmentNotFoundException ex) { return new ResponseEntity(ex.getMessage(), HttpStatus.NOT_FOUND); } }
विश्व स्तर पर अपवादों को संभालने के लिए, आप @ControllerAdvice और एक केंद्रीकृत अपवाद हैंडलर का उपयोग कर सकते हैं।
package com.example.SpringBootRefresher.error; import com.example.SpringBootRefresher.entity.ErrorMessage; import com.example.SpringBootRefresher.exception.DepartmentNotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @ControllerAdvice @ResponseStatus public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(DepartmentNotFoundException.class) public ResponseEntityhandleDepartmentNotFoundException(DepartmentNotFoundException exception, WebRequest request) { ErrorMessage message = new ErrorMessage( HttpStatus.NOT_FOUND.value(), exception.getMessage(), request.getDescription(false) ); return ResponseEntity.status(HttpStatus.NOT_FOUND) .body(message); } @ExceptionHandler(Exception.class) public ResponseEntity handleGlobalException(Exception exception, WebRequest request) { ErrorMessage message = new ErrorMessage( HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage(), request.getDescription(false) ); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(message); } }
अपने त्रुटि संदेशों को संरचित करने के लिए एक मानक त्रुटि प्रतिक्रिया वर्ग को परिभाषित करें।
package com.example.SpringBootRefresher.entity; public class ErrorMessage { private int statusCode; private String message; private String description; public ErrorMessage(int statusCode, String message, String description) { this.statusCode = statusCode; this.message = message; this.description = description; } // Getters and setters public int getStatusCode() { return statusCode; } public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
स्प्रिंग बूट बीन वैलिडेशन (जेएसआर-380) के साथ अच्छी तरह से एकीकृत होता है। विश्व स्तर पर सत्यापन त्रुटियों को संभालने के लिए, @ControllerAdvice का उपयोग करें।
package com.example.SpringBootRefresher.error; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.context.request.WebRequest; import java.util.HashMap; import java.util.Map; @ControllerAdvice @ResponseStatus public class ValidationExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity
सरल मामलों के लिए, आप HTTP स्थिति कोड निर्दिष्ट करने के लिए @ResponseStatus के साथ एक अपवाद वर्ग को एनोटेट कर सकते हैं।
package com.example.SpringBootRefresher.exception; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_FOUND) public class DepartmentNotFoundException extends RuntimeException { public DepartmentNotFoundException(String message) { super(message); } }
स्प्रिंग बूट में अपवाद प्रबंधन में त्रुटियों को प्रभावी ढंग से प्रबंधित करने के लिए @ExceptionHandler, @ControllerAdvice, और @ResponseStatus जैसे एनोटेशन का उपयोग करना शामिल है। कस्टम अपवाद बनाकर, सत्यापन त्रुटियों को संभालकर और सर्वोत्तम प्रथाओं का पालन करके, आप मजबूत एप्लिकेशन बना सकते हैं जो त्रुटियों को शालीनता से संभालते हैं और उपयोगकर्ताओं को सार्थक प्रतिक्रिया प्रदान करते हैं। जावा 17 सुविधाओं का उपयोग यह सुनिश्चित करता है कि आपका एप्लिकेशन जावा पारिस्थितिकी तंत्र में नवीनतम सुधारों का लाभ उठाता है।
अस्वीकरण: उपलब्ध कराए गए सभी संसाधन आंशिक रूप से इंटरनेट से हैं। यदि आपके कॉपीराइट या अन्य अधिकारों और हितों का कोई उल्लंघन होता है, तो कृपया विस्तृत कारण बताएं और कॉपीराइट या अधिकारों और हितों का प्रमाण प्रदान करें और फिर इसे ईमेल पर भेजें: [email protected] हम इसे आपके लिए यथाशीघ्र संभालेंगे।
Copyright© 2022 湘ICP备2022001581号-3