what is the right way to handle errors in spring-webflux

前端 未结 6 2074
轮回少年
轮回少年 2020-12-09 11:12

I\'ve been doing some research using spring-webflux and I like to understand what should be the right way to handle errors using Router Functions.

I\'ve created an s

6条回答
  •  误落风尘
    2020-12-09 11:42

    If you think, router functions are not the right place to handle exceptions, you throw HTTP Exceptions, that will result in the correct HTTP Error codes. For Spring-Boot (also webflux) this is:

      import org.springframework.http.HttpStatus;
      import org.springframework.web.server.ResponseStatusException;
      .
      .
      . 
    
      new ResponseStatusException(HttpStatus.NOT_FOUND,  "Collection not found");})
    

    spring securities AccessDeniedException will be handled correctly, too (403/401 response codes).

    If you have a microservice, and want to use REST for it, this can be a good option, since those http exceptions are quite close to business logic, and should be placed near the business logic in this case. And since in a microservice you shouldn't have to much businesslogic and exceptions, it shouldn't clutter your code, too... (but of course, it all depends).

提交回复
热议问题