@RestControllerAdvice Example SpringBoot
Tags: springboot, restcontrolleradvice, example
Published: 2021-01-04 13:54:39

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](https://loizenai.com/angular-8-springboot-mysql-crud-example/)
- [Angular 9 SpringBoot MySQL CRUD Example](https://loizenai.com/angular-9-springboot-mysql-crud-example/)
- [SpringBoot + Angular 10 PostgreSQL CRUD Example](https://loizenai.com/springboot-angular-10-postgresql-crud-restapi-example/)
- [Angular 8 SpringBoot PostgreSQL CRUD Example](https://loizenai.com/angular-8-springboot-postgresql-crud-example/)
- [SpringBoot Angular 9 PostgreSQL CRUD Example](https://loizenai.com/springboot-angular-9-postgresql-crud-example-architecture-diagram/)
- [Angular 11 SpringBoot CRUD MySQL Example](https://loizenai.com/angular-11-springboot-crud-mysql-example/)
- [Angular SpringBoot CRUD MySQL Example](https://loizenai.com/angular-springboot-crud-mysql-example/)
- [SpringBoot MongoDB Angular CRUD Example Github](https://loizenai.com/spring-boot-mongodb-angular-crud-example-github/)
- [How to Integrate Angular 9 with SpringBoot Tutorial](https://loizenai.com/how-to-integrate-angular-9-with-springboot-tutorial/)

## Youtube video

- https://www.youtube.com/watch?v=dTR-41_jMvc&t=46s
- https://www.youtube.com/watch?v=lb5LVzJbquI&t=476s
- https://www.youtube.com/watch?v=DoV8xfA8WBo&t=30s
- https://www.youtube.com/watch?v=rYmf_MthobU&t=376s
- https://www.youtube.com/watch?v=7ZfInOvFsz0&t=1308s