在JPA/Hibernate中添加自定义错误消息的更好方法是:

huangapple go评论122阅读模式
英文:

Better way to add custom error message during Unique Constrains JPA/Hibernate

问题

我认为在Spring Boot/Hibernate中有更好的方法来做这件事。有很多表格,我不想为每个表格都添加用户定义的模型。

我有一个实体:

  1. @Entity
  2. public class Item extends ExtraEntity {
  3. @NotEmpty(message = "Item code can not be Empty !")
  4. @Column(unique = true)
  5. private String code;
  6. @NotEmpty(message = "Item name can not be Empty !")
  7. @Column(unique = true)
  8. private String name;
  9. }

如何在我的REST API中发送自定义消息,指示代码重复或名称重复?

英文:

I think there is a better way to do this in Spring boot/hibernate. There are many tables and I don't want to add User Defined model for every table: here

I have an entity

  1. @Entity
  2. public class Item extends ExtraEntity {
  3. @NotEmpty(message = "Item code can not be Empty !")
  4. @Column(unique = true)
  5. private String code;
  6. @NotEmpty (message = "Item name can not be Empty !")
  7. @Column(unique = true)
  8. private String name;
  9. }

How do I send a custom message for my REST APIs stating if code is duplicate or name is duplicate?

答案1

得分: 2

在最外层捕获异常如下:

  1. try {
  2. newEntity = ourService.createUpdate(entity);
  3. } catch (JpaSystemException jpae) {
  4. if (jpae.getCause().getCause() instanceof ConstraintViolationException) {
  5. if (((ConstraintViolationException)jpae.getCause().getCause()).getConstraintName().equals("SCHEMA.UK_CODE_01")){
  6. throw new DuplicatedCodeException("Message",jpae);
  7. } else if (((ConstraintViolationException)jpae.getCause().getCause()).getConstraintName().equals("SCHEMA.UK_NAME_01")){
  8. throw new DuplicatedNameException("Message",jpae);
  9. }
  10. }
  11. }

为每个唯一键创建自定义异常,如下:

  1. public class DuplicatedNameException extends Exception {
  2. public DuplicatedNameException(String message){
  3. super(message);
  4. }
  5. public DuplicatedNameException(String message, Throwable anException){
  6. super(message, anException);
  7. }
  8. }

定义一个异常处理程序,扩展自ResponseEntityExceptionHandler类,并处理接收到的异常如下:

  1. @ExceptionHandler({ DuplicatedNameException.class })
  2. public ResponseEntity<Object> handleDuplicatedNameException(Exception anException, WebRequest request) {
  3. [...]
  4. return new ResponseEntity<>(anException, new HttpHeaders(), YOUR_HTTP_STATUS_WITH_CUSTOM_CODE_HERE);
  5. }

这个HTTPStatus是你应该在Web层检查以显示错误消息的地方。

英文:

In the outermost layer, catch the exception as follows:

  1. try {
  2. newEntity = ourService.createUpdate(entity);
  3. } catch (JpaSystemException jpae) {
  4. if (jpae.getCause().getCause() instanceof ConstraintViolationException) {
  5. if (((ConstraintViolationException)jpae.getCause().getCause()).getConstraintName().equals(&quot;SCHEMA.UK_CODE_01&quot;)){
  6. throw new DuplicatedCodeException(&quot;Message&quot;,jpae);
  7. } else if (((ConstraintViolationException)jpae.getCause().getCause()).getConstraintName().equals(&quot;SCHEMA.UK_NAME_01&quot;)){
  8. throw new DuplicatedNameException(&quot;Message&quot;,jpae);
  9. }
  10. }
  11. }

Create a custom exception for each unique key like this:

  1. public class DuplicatedNameException extends Exception {
  2. public DuplicatedNameException(String message){
  3. super(message);
  4. }
  5. public DuplicatedNameException(String message, Throwable anException){
  6. super(message, anException);
  7. }
  8. }

Define an exception handler that extends from the ResponseEntityExceptionHandler class and treats the received exception as follows:

  1. @ExceptionHandler({ DuplicatedNameException.class })
  2. public ResponseEntity&lt;Object&gt; handleDuplicatedNameException(Exception anException, WebRequest request) {
  3. [...]
  4. return new ResponseEntity&lt;&gt;(anException, new HttpHeaders(), YOUR_HTTP_STATUS_WITH_CUSTOM_CODE_HERE);
  5. }

This HTTPStatus is the one that you should check in the web layer to show the error message

huangapple
  • 本文由 发表于 2020年8月11日 13:26:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63352008.html
匿名

发表评论

匿名网友

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

确定