“@RestControllerAdvice Example SpringBoot
RestControllerAdvice
1
#springboot
3
Male avatar

loveprogramming viết ngày 04/01/2021

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

Youtube video

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

30 bài viết.
32 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 3 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 1 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
30 bài viết.
32 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!