英文:
Lombok in enum : @Setter is giving compile error in enum
问题
Error : @Setter is only supported on a class or a field.
lombok version in pom.xml = 1.18.12
want to make this ErrorMessages generic for any type of error.
Need to create getter and setter through lombok and access like : ErrorMessages.MISSING_REQUIRED_FIELD.getErrorMessage()
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public enum ErrorMessages {
MISSING_REQUIRED_FIELD("缺少必填字段。请查看必填字段的文档。"),
RECORD_ALREADY_EXISTS("记录已存在");
private String errorMessage;
// public String getErrorMessage() {
// return errorMessage;
// }
//
// public void setErrorMessage(String errorMessage) {
// this.errorMessage = errorMessage;
// }
ErrorMessages(String errorMessage) {
this.errorMessage = errorMessage;
}
}
英文:
Error : @Setter is only supported on a class or a field.
lombok version in pom.xml = 1.18.12
want to make this ErrorMessages generic for any type of error.
Need to create getter and setter through lombok and access like : ErrorMessages.MISSING_REQUIRED_FIELD.getErrorMessage()
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public enum ErrorMessages {
MISSING_REQUIRED_FIELD("Missing required field. Please check documentation for required fields."),
RECORD_ALREADY_EXISTS("Record already exists");
private String errorMessage;
// public String getErrorMessage() {
// return errorMessage;
// }
//
// public void setErrorMessage(String errorMessage) {
// this.errorMessage = errorMessage;
// }
ErrorMessages(String errorMessage) {
this.errorMessage = errorMessage;
}
}
</details>
# 答案1
**得分**: 1
这应该是一个`class`,而不是一个`enum`,如果你不希望数据被更改,否则请参考Joachim Sauer的评论。
另外,请注意构造函数也需要是public的。
```java
@Setter
@Getter
public class ErrorMessages {
private String errorMessage;
public ErrorMessages(String errorMessage) {
this.errorMessage = errorMessage;
}
}
英文:
this should be a class
, not an enum
, if you wan't the data to be changed otherwise see Joachim Sauer comment.
Also please note the constructor need to be public as well
@Setter
@Getter
public class ErrorMessages {
private String errorMessage;
public ErrorMessages(String errorMessage) {
this.errorMessage = errorMessage;}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论