मैं बसंत के लिए नया हूं। मैं बसंत वेबएमवीसी के साथ एक आरईएसटी एपीआई विकसित कर रहा हूँ। त्रुटि प्रबंधन के लिए मुझे यह लिंक मिला है 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>
क्या आपको कभी भी आपकी समस्या का समाधान मिला है? – woemler
मुझे एक ही समस्या मिली ... –
मुझे लगता है कि RestResponseEntityExceptionHandler में आपके तरीके केवल तभी उपयोग किए जाएंगे जब वे ResponseEntityExceptionHandler से विधियों को ओवरराइड करते हैं। –