如何在Spring Boot中针对ObjectOptimisticLockingFailureException获得状态码409?

huangapple 未分类评论81阅读模式
英文:

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&lt;ModelAndView&gt; handleConflict(HttpServletRequest request, Exception e) {
    ModelAndView result = new ModelAndView(&quot;error/409&quot;);
    result.addObject(&quot;url&quot;, request.getRequestURL());
	return new ResponseEntity&lt;&gt;(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=&quot;simpleMappingExceptionResolver&quot;)
public SimpleMappingExceptionResolver
              createSimpleMappingExceptionResolver() {
SimpleMappingExceptionResolver r =
            new SimpleMappingExceptionResolver();

Properties mappings = new Properties();
mappings.setProperty(&quot;ObjectOptimisticLockingFailureException&quot;, &quot;error/409&quot;);


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>



huangapple
  • 本文由 发表于 2020年8月15日 02:17:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/63418088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定