• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Spring Boot Custom Exception Handling

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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() {

   }
}
 
Bartender
Posts: 2266
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to modify into this:
Content minimized. Click to view
 
Himai Minh
Bartender
Posts: 2266
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, you can commit your code to Github and let us see....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic