2014-06-21 5 views
8

मैं बसंत के लिए नया हूं। मैं बसंत वेबएमवीसी के साथ एक आरईएसटी एपीआई विकसित कर रहा हूँ। त्रुटि प्रबंधन के लिए मुझे यह लिंक मिला है http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-rest-spring-mvc-exceptionsResponseEntityExceptionHandler को तब नहीं कहा जा रहा है जब अपवाद होता है

मैंने अपनी परियोजना में ResponseEntityExceptionHandler का उपयोग करने का प्रयास किया है। लेकिन जब भी मेरा नियंत्रक अपवाद फेंकता है तो यह कभी भी इस ResponseEntityExceptionHandler तक नहीं पहुंचता है।

के बाद मेरी कोड स्निपेट

नियंत्रक

@Controller 
@RequestMapping("/hello") 
public class HelloController { 
    private static final Logger logger = Logger.getLogger(HelloController.class); 
    @RequestMapping(value="/{name}", method=RequestMethod.GET) 
    public @ResponseBody String greet(@PathVariable(value = "name")String name) throws InvalidInputException, ResourceNotFoundException{ 
     logger.info("start greet() "+name); 
     System.out.println("start greet() "+name); 
     String message = null; 
     if("".equalsIgnoreCase(name)) 
     { 
      throw new InvalidInputException("Invalid Input"); 
     } 
     List<String> names = new ArrayList<String>(); 
     names.add("Harshal"); 
     names.add("Smitesh"); 
     if(names.contains(name)){ 
      message = "Hello "+ name; 
     }else{ 
      throw new ResourceNotFoundException("Requested Resource not found"); 
     } 
     System.out.println("end greet"); 
     logger.info("end greet()"); 
     return message; 
    } 
} 

अपवाद

package com.practice.errorhandlerdemo.exception; 

public class InvalidInputException extends RuntimeException{ 
    private static final long serialVersionUID = 5489516240608806490L; 
    public InvalidInputException() { 
     super("Invalid Input"); 
    } 
    public InvalidInputException(String message) { 
     super(message); 
    } 
} 

package com.practice.errorhandlerdemo.exception; 

public class ResourceNotFoundException extends RuntimeException { 
    private static final long serialVersionUID = -4041009155673754859L; 
    public ResourceNotFoundException() { 
     super("requested resource not found"); 
    } 
    public ResourceNotFoundException(String message) { 
     super(message); 
    } 
} 

exceptionhandler हैं

@ControllerAdvice 
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { 
    private static final Logger logger = Logger.getLogger(RestResponseEntityExceptionHandler.class); 
    @ExceptionHandler(value={ResourceNotFoundException.class}) 
    @ResponseStatus(value=HttpStatus.NOT_FOUND) 
    protected ResponseEntity<Object> handleResourceNotFound(RuntimeException ex, WebRequest request){ 
     logger.info("start handleResourceNotFound()"); 
     String bodyOfResponse = "Requested resource does not found"; 
     HttpHeaders httpHeaders = new HttpHeaders(); 
     httpHeaders.setContentType(MediaType.APPLICATION_JSON); 
     return handleExceptionInternal(ex, bodyOfResponse, httpHeaders, HttpStatus.NOT_FOUND, request); 
    } 

    @ExceptionHandler(value={InvalidInputException.class}) 
    @ResponseStatus(value=HttpStatus.BAD_REQUEST) 
    protected ResponseEntity<Object> handleInvalidInput(RuntimeException ex, WebRequest request){ 
     logger.info("start handleInvalidInput()"); 
     String bodyOfResponse = "Invalid Input"; 
     HttpHeaders httpHeaders = new HttpHeaders(); 
     httpHeaders.setContentType(MediaType.APPLICATION_JSON); 
     return handleExceptionInternal(ex, bodyOfResponse, httpHeaders, HttpStatus.BAD_REQUEST, request); 
    } 
} 

डिस्पैचर सर्वलेट

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <context:component-scan base-package="com.practice.errorhandlerdemo.controller"/> 
    <context:annotation-config/> 

</beans> 

web.xml

<web-app> 
    <display-name>ErrorHandlerDemo</display-name> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/my-servlet.xml</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

क्या आपको कभी भी आपकी समस्या का समाधान मिला है? – woemler

+0

मुझे एक ही समस्या मिली ... –

+0

मुझे लगता है कि RestResponseEntityExceptionHandler में आपके तरीके केवल तभी उपयोग किए जाएंगे जब वे ResponseEntityExceptionHandler से विधियों को ओवरराइड करते हैं। –

उत्तर

3

आप ResponseEntityExceptionHandler विस्तार करने के लिए यदि आप सभी अपवाद मैपिंग यह प्रदान करता है की जरूरत नहीं है की जरूरत नहीं है।

एक आसान तरीका अपने अपवाद हैंडलिंग लिखने के लिए:

@ControllerAdvice 
public class RestResponseEntityExceptionHandler { 

    @ExceptionHandler(ResourceNotFoundException.class) 
    protected ResponseEntity<String> handleResourceNotFound(ResourceNotFoundException ex){ 

     return ResponseEntity 
       .status(HttpStatus.NOT_FOUND) 
       .body("Requested resource does not found"); 
    } 

    @ExceptionHandler(InvalidInputException.class) 
    protected ResponseEntity<String> handleInvalidInput(InvalidInputException ex){ 

     return ResponseEntity 
       .badRequest() 
       .body("Invalid Input"); 
    } 
} 

ध्यान दें कि ResponseEntity बिल्डर एपीआई स्प्रिंग 4.1 में प्रस्तुत किया गया है, लेकिन आप 4.0.x. पर नियमित रूप से निर्माता का उपयोग कर सकते

+0

काम करता है कोई भी स्पष्टीकरण के बिना इस उत्तर को क्यों कम करेगा? 'ResponseEntityExceptionHandler' अतिरिक्त हैंडलर लाता है लेकिन आपको इसे विस्तारित करने की आवश्यकता नहीं है। कुंजी '@ नियंत्रक एडवाइस' होना है और अपना स्वयं का '@ अपवाद हैंडलर' एनोटेटेड विधियां लिखना है। – amertkara

+0

मैंने इसे घटा दिया क्योंकि सवाल यह है कि ResponseEntityExceptionHandler कैसे काम करना है, मुझे पहले स्थान पर एक की आवश्यकता नहीं है। इस उत्तर में 'अगर', दिखाता है कि यह गलत है और यहां नहीं होना चाहिए – dezzer10

+0

सुरक्षा अपवादों के बारे में क्या? –

1

मुझे स्प्रिंग वेबएमवीसी 4.2.5 में एक ही समस्या मिली। कारण DispatcherServlet का पैरामीटर था। डिफ़ॉल्ट रूप से यह मान false है, इसलिए सभी त्रुटियां HttpServletResponse.SC_NOT_FOUND सर्वलेट प्रतिक्रिया उत्पन्न करती हैं और कोई अपवाद फेंकता नहीं है।

मैं इसे सही पर सेट के बाद, मेरे @ExceptionHandlers काम करने के लिए

+0

मेरे लिए भी काम करता है .. धन्यवाद –

0

कुछ स्थितियों में, जहां दोनों ResponseEntityExceptionHandler और @ControllerAdvice काम नहीं किया सूचना दी रहे हैं शुरू कर दिया।

दोनों को कक्षा के तहत @ExceptionHandler के साथ एन्क्रिप्टेड विधियों को संकलित करना माना जाता है जहां सभी नियंत्रक संदर्भित कर सकते हैं।

यदि यह आपके लिए काम नहीं करता है। आप @ExceptionHandler विधियों को एक सामान्य AbstractController कक्षा में जोड़ सकते हैं जो अन्य सभी नियंत्रकों द्वारा विस्तारित किया जाता है।

संबंधित मुद्दे