- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道为什么,但 @ControllerAdvice
覆盖在 Exception
处定义的响应代码级别使用 @ResponseStatus
注解。
异常(exception) :
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GreetException extends RuntimeException {}
@RestController
@RequestMapping("/")
public class GreetController {
@RequestMapping(method = RequestMethod.GET)
public String greet() {
throw new GreetException();
}
}
@ControllerAdvice
public class ExceptionConfiguration {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(RuntimeException.class)
public void handleConflict() {}
}
GreetController
被调用响应是 409 - CONFLICT。因为我专门提供了异常级别的响应代码,所以我预计这将是返回的代码(400 - BAD_REQUEST)。
RuntimeException
定义了一个 Controller 建议。定义,以便我们可以为每个未捕获的异常分配一个 id。
最佳答案
用 @ResponseStatus
注释异常的问题也就是说,这仅在其他地方未处理异常时触发。
请参阅来自 Spring article about exception handling in Spring MVC 的引用:
When an annotated exception is thrown from a controller method, and not handled elsewhere, it will automatically cause the appropriate HTTP response to be returned with the specified status-code.
ControllerAdvice
使其工作的方法。与一般处理程序。见
Paragraph about ControllerAdvice .
@ResponseStatus
注释,您基本上要做的是在通用处理程序中重新抛出异常。 :
@ControllerAdvice
public class ExceptionConfiguration {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(RuntimeException.class)
public void handleConflict(RuntimeException e) throws RuntimeException {
// Check for annotation
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
// Rethrow Exception
throw e;
}
else {
// Provide your general exception handling here
}
}
}
关于spring - @ControllerAdvice 覆盖异常 @ResponseStatus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460932/
我可以定义一个 Spring ControllerAdvice,它由使用自定义注解的 Controller 子集有选择地使用: @RestController @UseAdviceA @Request
这是 web 入口点的主 Controller @Controller @RequestMapping("/webapp") public class WebAppController { @Requ
我有一个 Spring 4 应用程序,其中包含多个用 @Order(someValue) 注释的 ControllerAdvices。此外,我在我的一个外部库中发现了一个 ControllerAdvi
实现前的准备工作 @ControllerAdvice注解的简单介绍 @ControllerAdvice注解作用是给Controller控制器添加统一的操作或处理。 对于@ControllerAdvic
我一直在关注一些有关使用 Spring 框架进行错误处理的教程。我的handleNullPointer方法确实捕获了空指针异常。但是,我想知道该方法之后返回到哪里,因为我想在前端(网页)上显示错误的一
有没有办法从 @ControllerAdvice 获得控制权的地方获取类。 即:如果 PersonController 的执行正在进行,并且我收到一些错误,由于控制权转移到 @ControllerAd
我有一个用 @ControllerAdvice 注释的类,为我的 api 提供了一些中央异常处理。它处理的异常之一是 MethodArgumentNotValidException,当用 @Valid
我想将响应正文设置为异常的错误消息: @ControllerAdvice public class RestResponseEntityExceptionHandler extends Respons
实际上,我在一个单独的项目中使用 @ControllerAdvice 为我的 Sprint 启动项目编写了一个全局 ExceptionHandling,现在我想从另一个项目调用它,但似乎它不起作用,因
我有一个 REST 服务,我需要使用自定义 json 响应发送适当的状态代码,这解释了错误的原因。 我有@Controller类,它公开了一个服务/test,在访问该服务时,我抛出了一个自定义异常(扩
我已经浏览了所有相关的帖子,但我的@ControllerAdvice似乎没有处理从 Controller 类抛出的自定义异常。但是@Controller类中的@ExceptionHandler确实处理
我正在开发一个 Spring MVC/Webflow 应用程序(版本 3.2)并尝试让异常处理工作,我可以在其中将自定义异常消息输出到日志文件和 error.jsp。我遇到的问题是异常处理程序没有被解
我正在为 Spring MVC 中的异常处理开发示例演示应用程序。我正在尝试使用@ControllerAdvice 进行异常处理 我按照 this 中描述的步骤进行操作链接。 但是当我运行我的应用程序
我有以下 Controller 类 package com.java.rest.controllers; @Controller @RequestMapping("/api") public clas
在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@Req
我有 SimpleMappingExceptionResolver像这样配置。 @Bean(name = "simpleMappingExceptionResolver") public Simple
我不知道为什么,但 @ControllerAdvice覆盖在 Exception 处定义的响应代码级别使用 @ResponseStatus注解。 异常(exception) : @ResponseSt
我制作了一个用于监听异常的通用项目并使用 @ControllerAdvice与 @ExceptionHandler @ControllerAdvice(annotations = RestContro
在我的 REST 服务应用程序中,我计划创建一个 @ControllerAdvice捕获 Controller 抛出的异常并返回 ResponseEntity 的类对象根据错误类型。 但我已经有一个
@RestControllerAdvice 和 @ControllerAdvice 之间的主要区别是什么? 我们是否应该始终将 @RestControllerAdvice 用于休息服务和 @Contr
我是一名优秀的程序员,十分优秀!