https://loizenai.com/restcontrolleradvice-springboot-example/
Tutorial: “@RestControllerAdvice Example SpringBoot – Error Handling for REST with Spring”.
In the article, I introduce about @RestControllerAdvice annotation of SpringBoot to handle RestAPI exception with a Github running sourcecode and details explanation example steps.
Read more: https://loizenai.com/angular-10-spring-boot-jwt-authentication-example/
https://dev.to/ozenero/angular-11-springboot-jwt-authentication-example-spring-security-mysql-20b8
- Technologies we use in the article: – Java 1.8 – Maven
Annotation Type @RestControllerAdvice
@RestControllerAdvice is a new feature of Spring Framework 4.3, an annotation with combined @ControllerAdvice + @ResponseBody. So @RestControllerAdvice can help us to handle Exception with RestfulApi by a cross-cutting concern solution: @ExceptionHandler.
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@ControllerAdvice
@ResponseBody
public @interface RestControllerAdvice
@RestControllerAdvice is processed if an appropriate HandlerMapping-HandlerAdapter pair is configured such as the RequestMappingHandlerMapping-RequestMappingHandlerAdapter pair which are the default in the MVC Java config and the MVC namespace.
@RestControllerAdvice
public class WebRestControllerAdvice {
@ExceptionHandler(CustomNotFoundException.class)
public ResponseMsg handleNotFoundException(CustomNotFoundException ex) {
ResponseMsg responseMsg = new ResponseMsg(ex.getMessage());
return responseMsg;
}
}
The handleNotFoundException method will handle all exceptions has type: CustomNotFoundException from any @RequestMapping like:
@RequestMapping(value="/customer/{name}")
public Customer findCustomerByName(@PathVariable("name")String name){
Customer cust = customerService.findCustomerByName(name);
if(null == cust){
throw new CustomNotFoundException("Not found customer with name is " + name);
}
return cust;
}
Related post
- Angular 8 SpringBoot MySQL CRUD Example
- Angular 9 SpringBoot MySQL CRUD Example
- SpringBoot + Angular 10 PostgreSQL CRUD Example
- Angular 8 SpringBoot PostgreSQL CRUD Example
- SpringBoot Angular 9 PostgreSQL CRUD Example
- Angular 11 SpringBoot CRUD MySQL Example
- Angular SpringBoot CRUD MySQL Example
- SpringBoot MongoDB Angular CRUD Example Github
- How to Integrate Angular 9 with SpringBoot Tutorial
Top comments (0)