• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Spring Boot Custom Exception Handling

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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() {

   }
}
 
Ranch Foreman
Posts: 1897
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to modify into this:
Content minimized. Click to view
 
Himai Minh
Ranch Foreman
Posts: 1897
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe, you can commit your code to Github and let us see....
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic