Hi JavaLovers,
I am trying to display customized error message in the response in spring boot rest . But it's not showing message in the response and handleAllException method is also not being called at all. Can you please some help me.
Thank in Advance
ProductController.java
@RestController
public class ProductController {
@RequestMapping(value = "/say/{hello}",method = RequestMethod.GET)
public void sayHello(@PathVariable
String hello) throws ProductNotFoundException {
if(hello.contains("a")){
throw new ProductNotFoundException();
}else{
System.out.println("Hello..."+hello);
}
}
}
ProductExceptionHandler.java
@ControllerAdvice
public class ProductExceptionHandler {
@ExceptionHandler({ProductNotFoundException.class})
public ResponseEntity<Object> handleAllExceptions(ProductNotFoundException ex) {
return new ResponseEntity<>("Hello Exception", HttpStatus.NOT_FOUND);
}
}
ProductNotFoundException.java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProductNotFoundException extends Exception{
private static final long serialVersionUID = 1L;
public ProductNotFoundException() {
}
}