英文:
How to get a 409 for ObjectOptimisticLockingFailureException with Spring Boot?
问题
我知道Spring Boot默认会为导致状态码为409 CONFLICT的异常呈现Thymeleaf模板,模板位于error/409.html
。然而,默认情况下,ObjectOptimisticLockingFailureException
会导致500 SERVER ERROR。
我可以使用@ControllerAdvice
来处理异常,然后"手动"重定向到error/409.html
模板,就像下面这样:
@ControllerAdvice
public class GlobalControllerAdvice {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler({DataIntegrityViolationException.class, ObjectOptimisticLockingFailureException.class})
public ModelAndView handleConflict(HttpServletRequest request, Exception e) {
ModelAndView result = new ModelAndView("error/409");
result.addObject("url", request.getRequestURL());
return result;
}
}
由于ObjectOptimisticLockingFailureException
不是我自己的代码的一部分,我无法使用@ResponseStatus(HttpStatus.CONFLICT)
来注解它以使其返回409状态码。
是否可以将ObjectOptimisticLockingFailureException
映射到409并在Spring Boot中使用默认的错误模板机制?
英文:
I know that Spring Boot will render a Thymeleaf template at error/409.html
for an exception that leads to a statuscode of 409 CONFLICT by default. However, it seems that ObjectOptimisticLockingFailureException
gives a 500 SERVER ERROR by default.
I am able to handle the exception and "manually" redirect to the error/409.html
template using a @ControllerAdvice
like this:
@ControllerAdvice
public class GlobalControllerAdvice {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler({DataIntegrityViolationException.class, ObjectOptimisticLockingFailureException.class})
public ModelAndView handleConflict(HttpServletRequest request, Exception e) {
ModelAndView result = new ModelAndView("error/409");
result.addObject("url", request.getRequestURL());
return result;
}
}
Since ObjectOptimisticLockingFailureException
is not part of my own code, I cannot annotate it with @ResponseStatus(HttpStatus.CONFLICT)
to have a 409.
Is it possible to map ObjectOptimisticLockingFailureException
to 409 and use the default error template mechanism for error codes in Spring Boot?
答案1
得分: 0
我们可以尝试使用以下的 ResponseEntity:
@ExceptionHandler({DataIntegrityViolationException.class, ObjectOptimisticLockingFailureException.class})
public ResponseEntity<ModelAndView> handleConflict(HttpServletRequest request, Exception e) {
ModelAndView result = new ModelAndView("error/409");
result.addObject("url", request.getRequestURL());
return new ResponseEntity<>(result, HttpStatus.CONFLICT);
}
英文:
We can try with ResponseEntity as below
@ExceptionHandler({DataIntegrityViolationException.class, ObjectOptimisticLockingFailureException.class})
public ResponseEntity<ModelAndView> handleConflict(HttpServletRequest request, Exception e) {
ModelAndView result = new ModelAndView("error/409");
result.addObject("url", request.getRequestURL());
return new ResponseEntity<>(result, HttpStatus.CONFLICT);
}
答案2
得分: 0
这可能会起作用
@Bean(name="simpleMappingExceptionResolver")
public SimpleMappingExceptionResolver
createSimpleMappingExceptionResolver() {
SimpleMappingExceptionResolver r =
new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
mappings.setProperty("ObjectOptimisticLockingFailureException", "error/409");
r.setExceptionMappings(mappings);
return r;
}
<details>
<summary>英文:</summary>
This might work
@Bean(name="simpleMappingExceptionResolver")
public SimpleMappingExceptionResolver
createSimpleMappingExceptionResolver() {
SimpleMappingExceptionResolver r =
new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
mappings.setProperty("ObjectOptimisticLockingFailureException", "error/409");
r.setExceptionMappings(mappings);
return r;
}
</details>
# 答案3
**得分**: 0
可以捕获 `ObjectOptimisticLockingFailureException` 异常,并重新抛出一个状态码为 409 的异常。
```java
try {
// 在此处执行你的任务
} catch (ObjectOptimisticLockingFailureException e) {
var ex = new ClientErrorException(409, e);
throw ex;
}
英文:
Could you catch the exception ObjectOptimisticLockingFailureException
and re-throw an exception with 409?
try {
//perform your task here
} catch (ObjectOptimisticLockingFailureException e) {
var ex = new ClientErrorException(409, e);
throw ex;
}
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论