Spring @RestControllerAdvice vs@ControllerAdvice @RestControllerAdvice和@ControllerAdvice之间的主要区别是什么 我们是否应该始终为rest服务使用@RestControllerAdvice和@ControllerAdvice MVC @RestControllerAdvice只是@ControllerAdvice+@ResponseBody的一个语法糖,您可以查看
我们是否应该始终使用@RestControllerAdvice来提供rest服务和服务 @ControllerAdvice MVCSpring @RestControllerAdvice vs@ControllerAdvice @RestControllerAdvice和@ControllerAdvice之间的主要区别是什么 我们是否应该始终为rest服务使用@RestControllerAdvice和@ControllerAdvice MVC @RestControllerAdvice只是@ControllerAdvice+@ResponseBody的一个语法糖,您可以查看,spring,Spring,我们是否应该始终使用@RestControllerAdvice来提供rest服务和服务 @ControllerAdvice MVC 同样,如上所述,@ControllerAdvice甚至可以用于REST web服务,但您还需要另外使用@ResponseBody此外,我们可以将其理解为: @RestControler=@Controller+@ResponseBody @RestControllerAdvice=@ControllerAdvice+@ResponseBody 请记住,@RestCo
同样,如上所述,
@ControllerAdvice
甚至可以用于REST web服务,但您还需要另外使用@ResponseBody
此外,我们可以将其理解为:
@RestControler
=@Controller
+@ResponseBody
@RestControllerAdvice
=@ControllerAdvice
+@ResponseBody
请记住,@RestControllerAdvice
是使用RestfulApi处理异常的更方便的注释
操作系统使用示例:
@RestControllerAdvice
public class WebRestControllerAdvice {
@ExceptionHandler(CustomNotFoundException.class)
public ResponseMsg handleNotFoundException(CustomNotFoundException ex) {
ResponseMsg responseMsg = new ResponseMsg(ex.getMessage());
return responseMsg;
}
}
在这种情况下,CustomNotFoundException的任何异常实例都将在响应体中抛出
此处提取的示例:
异常:一个好的RESTAPI应该正确处理异常,并向用户发送正确的响应。用户不应呈现任何未处理的异常。
RESTAPI开发人员将有两个与错误处理相关的需求
GlobalExceptionHandler是用@ControllerAdvice注释的,因此它将截获应用程序中来自控制器的异常。您能给我们举个例子吗?您在这句话中的意思是“@RestControllerAdvice”:“请记住,@ControllerAdvice是用RestfulApi处理异常更方便的注释。”@是的,你是对的,谢谢你的提示。