在枚举中使用 Lombok:@Setter 在枚举中会导致编译错误。

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

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;}
}

huangapple
  • 本文由 发表于 2020年8月7日 05:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63292022.html
匿名

发表评论

匿名网友

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

确定