Enumeration type definition
-
Simple enumeration
public enum Day0 { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; } Copy the code
-
Customize properties and methods to enumerations
(1) Customize a day attribute
@Getter public enum Day1 { MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); private String day; Day1(String day) { this.day = day; }}Copy the code
(2) User-defined two attributes
@Getter public enum Day { MONDAY(0."Monday"), TUESDAY(1."Tuesday"), WEDNESDAY(2."Wednesday"), THURSDAY(3."Thursday"), FRIDAY(4."Friday"), SATURDAY(5."Saturday"), SUNDAY(6."Sunday"); private int val; private String day; Day(int val, String day) { // Enumeration type constructors must be private and do not need to be specified this.val = val; this.day = day; }}Copy the code
How enumerated types are implemented
After the enum type is created using the keyword enum and compiled, the compiler generates an associated class for us that inherits from the java.lang.enum class.
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
// Enumerate constant names, such as MONDAY, TUESDAY
private final String name;
public final String name(a) {
return name;
}
// The ordinal of the enumeration, starting from 0
private final int ordinal;
public final int ordinal(a) {
return ordinal;
}
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
public String toString(a) {
return name;
}
public static <T extends Enum<T>> T valueOf(Class
enumType, String name)
{
T result = enumType.enumConstantDirectory().get(name);
if(result ! =null)
return result;
if (name == null)
throw new NullPointerException("Name is null");
throw new IllegalArgumentException(
"No enum constant " + enumType.getCanonicalName() + "."+ name); }... }Copy the code
Javap decompile enumeration type Day:
public final class com.tplink.test.pojo.Day extends java.lang.Enum<com.tplink.test.pojo.Day> {
public static final com.tplink.test.pojo.Day MONDAY;
public static final com.tplink.test.pojo.Day TUESDAY;
public static final com.tplink.test.pojo.Day WEDNESDAY;
public static final com.tplink.test.pojo.Day THURSDAY;
public static final com.tplink.test.pojo.Day FRIDAY;
public static final com.tplink.test.pojo.Day SATURDAY;
public static final com.tplink.test.pojo.Day SUNDAY;
public static com.tplink.test.pojo.Day[] values(); // The method added by the compiler
public static com.tplink.test.pojo.Day valueOf(java.lang.String); // The compiler adds a method to look up objects by name
public int getVal(a); // Customize the getter
public java.lang.String getDay(a); // Customize the getter
static {};
}
Copy the code
Default conversion
The default enumeration type conversion can only pass ordinal and name of the enumeration type.
Such as:
// restcontroller
@PostMapping("/day1")
public void day1(@RequestBody XxVo vo) {... }// VO
@Data
public class XxVo {
private Day1 day1; Day1 is the enumerated class defined above
}
Copy the code
Can receive:
// ordinal
{
"day1": 0
}
// name
{
"day1": "MONDAY"
}
Copy the code
Cannot receive:
{
"day1": "Monday"
}
Copy the code
The following describes the custom transformation method.
Enumeration type conversions in RequestBody
RequestBody parsing depends on HttpMessageConverter.
Use Jackson’s @jsonValue and @JsonCreator annotations.
@Getter
public enum Day {
MONDAY(1."monday"),
TUESDAY(2."tuesday"),
WEDNESDAY(3."wednesday"),
THURSDAY(4."thursday"),
FRIDAY(5."friday"),
SATURDAY(6."saturday"),
SUNDAY(-1."sunday");
private Integer value;
private String day;
Day(int value, String day) {
this.value = value;
this.day = day;
}
@JsonValue
public Integer getValue(a) {
return this.value;
}
@JsonCreator
public static Day Day(Integer value) {
for (Day day : values()) {
if (day.value.equals(value)) {
returnday; }}return null; }}Copy the code
Enumeration type conversion in RequestParam
Parameter conversion in RequestParam relies on the WebDataBinder mechanism.
Define a BaseEnum interface, customize Converter and register with WebMvcConfigurer.
public interface BaseEnum {
/** * serialize *@return* /
Object toValue(a);
/** * deserialize *@paramEnumType Actual enumeration type *@paramValue Current value *@param<T> enumerates types and implements the current interface *@returnEnumeration constant */
static <T extends Enum<T> & BaseEnum> T valueOf(Class<T> enumType, Object value) {
if (enumType == null || value == null) {
return null;
}
T[] enumConstants = enumType.getEnumConstants();
for (T enumConstant : enumConstants) {
Object enumValue = enumConstant.toValue();
if (Objects.equals(enumValue, value)
|| Objects.equals(enumValue.toString(), value.toString())) {
returnenumConstant; }}return null; }}Copy the code
@SuppressWarnings({"unchecked", "rawtypes"})
public class StringToEnumConverterFactory implements ConverterFactory<String.BaseEnum> {
@Override
public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnum(targetType);
}
private class StringToEnum<T extends Enum<T> & BaseEnum> implements Converter<String.T> {
private final Class<T> enumType;
public StringToEnum(Class<T> enumType) {
this.enumType = enumType;
}
@Override
public T convert(String source) {
if (source.isEmpty()) {
return null;
}
return (T) BaseEnum.valueOf(this.enumType, source.trim()); }}}Copy the code
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(newStringToEnumConverterFactory()); }}Copy the code
Enumerated type serialization
Use Jackson’s @jsonFormat annotation, not at the same time as @jsonValue.
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Day {
MONDAY(0, "monday"),
TUESDAY(1, "tuesday"),
WEDNESDAY(2, "wednesday"),
THURSDAY(3, "thursday"),
FRIDAY(4, "friday"),
SATURDAY(5, "saturday"),
SUNDAY(6, "sunday"); private int val; private String day; Day(int val, String day) { this.val = val; this.day = day; }}Copy the code
Can be serialized as follows:
{
"val": 0."day": "monday"
}
Copy the code