java—如何在集成不同部分时使用枚举使响应一致

tyg4sfes  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(116)

我正在研究 validate api中,我调用 validate 库类的方法,该方法返回 List<ModelProblem> 以及我在服务中定义的其他几个验证。
中国的阶级结构 ModelProblem 在库中定义如下:

public class ModelProblem {
  private final ModelProblemCode modelProblemCode;
  private final String message;
  private final List<String> offendingElements;

  public ModelProblem(ModelProblemCode modelProblemCode, String message) {
    this(modelProblemCode, message, (Collection)null);
  }

  public ModelProblem(ModelProblemCode modelProblemCode, String message, Collection<String> offendingElements) {
    this.offendingElements = new ArrayList();
    this.modelProblemCode = modelProblemCode;
    this.message = message;
    if (offendingElements != null) {
      this.offendingElements.addAll(offendingElements);
    }

  }

  public ModelProblemCode getModelProblemCode() {
    return this.modelProblemCode;
  }

  public String getMessage() {
    return this.message;
  }

  public List<String> getOffendingElements() {
    return new ArrayList(this.offendingElements);
  }
}
``` `ModelProblemCode` 枚举在库中定义如下:

public enum ModelProblemCode {

ENUM1("enum message 1"),
ENUM2("enum message 2"),
ENUM3("enum message 3"),
ENUM4("enum message 4"),
ENUM5("enum message 5"),
ENUM6("enum message 6");

private final String message;

private ModelProblemCode(String message) {
this.message = message
}

public String getMessage() {
return this.message;
}
}

在 `validate` api,我必须发送所有类型的验证错误作为响应。
为此,我需要为服务中定义的每种验证类型定义一个错误代码,并且我不能继承现有的枚举 `ModelProblemCode` ,因此我定义了一个自定义冲突类,如下所示:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyCustomViolation {

private final ModelProblem modelProblem; //this came from Library
private final MyCustomViolationCode myCustomViolationCode;
private final String message;

public MyCustomViolation(ModelProblem modelProblem,
MyCustomViolationCode MyCustomViolationCode,
String message) {
this.modelProblem = modelProblem;
this.myCustomViolationCode = myCustomViolationCode;
this.message = message;
}

public MyCustomViolation(MyCustomViolationCode myCustomViolationCode, String message) {
this(null, myCustomViolationCode, message);
}

public MyCustomViolation(ModelProblem modelProblem) {
this(modelProblem, null, null);
}

public ModelProblem getModelProblem() {
return this.modelProblem;
}

public MyCustomViolationCode getMyCustomViolationCode() {
return this.myCustomViolationCode;
}

public String getMessage() {
return this.message;
}
}
``` MyCustomViolationCode 枚举定义如下:

public enum MyCustomViolationCode {
  CUSTOM_ENUM1("custom message");

  private final String message;
  private MyCustomViolationCode(String message) {
    this.message = message;
  }

  public String getMessage() {
    return this.message;
  }
}

在回应 validate api,其中我已经集成了两个验证器(一个库和一个我在服务中定义的自定义验证器),我将响应作为 List<MyCustomViolation> 具体如下:

[
  { //THIS ONE IS COMING FROM VALIDATOR DEFINED IN THE LIBRARY
    "modelProblem": {                    
      "modelProblemCode": "<model problem code>,
      "message": "<model problem error message>",
      "offendingElements": [
        <offendingElement1>
      ]
    }
  },
  {   //THIS ONE IS COMING FROM ONE OF CUSTOM VALIDATOR THAT I DEFINED
    "myCustomViolationCode": "<custom violation code>",
    "message": "<custom violation error message>"
  }
]

所以,React看起来并不好。我的意思是它在所有被调用的验证器中看起来并不一致 validate 应用程序编程接口。我不知道该怎么做。
有人能帮忙吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题