Explanation of Java Web enumeration - super detailed!!!

Keywords: Java Spring

Enumeration usage problems

​ In web development, enumeration is usually used as status code, code table value, etc., so we often use it in the three-tier architecture. We pass values through the front end, traverse enumeration items, and compare them manually one by one. Manual comparison is still cumbersome. Here is the method of automatic framework installation and replacement to simplify daily development!

Jackson enumeration simplifies operations

​ By default, springBoot integrates the Jackson serializer, which has the following two annotations to solve the serialization and deserialization of enumeration.

1, @ JsonCreator

This annotation is used to deserialize enumeration values into enumeration objects. It is usually used to input parameters at the Controller layer, but the input parameters in this way can only be requests with a request header of ContentType of application/json. Nothing else works. For details, you can learn about the following springboot and jackson, which will not be explained in detail here. If you want to convert the enumeration value in the form request into enumeration, you can only do it through the Spring Converter, which will be explained in detail later.

2, @ JsonValue

The secondary annotation is used to serialize the object and is marked on the get method. After marking, the value of the get method will be returned

The code is as follows:

public class ConstantUtils {
    public enum Demo{
        TEST1(1,"I am a"),
        TEST2(2,"I'm two"),
        TEST3(3,"I'm three"),
        TEST4(4,"I'm four"),
        TEST5(5,"I'm five"),
        TEST6(6,"I'm six")
        ;
        Demo(int code,String name){
            this.code = code;
            this.name = name;
        }
        public int code;
        private String name;

        @JsonCreator   
        public static Demo getDemo(int code){
            for (Demo value : values()) {
                if (value.code == code) {
                    return value;
                }
            }
            return null;
        }

        @JsonValue
        public String getName() {
            return name;
        }
    }
}

Mybatis enumeration simplifies operations

​ Here is an example of mybatis plus, a derivative of my commonly used mybatis. mp (mybatis plus), it is very simple to simplify enumeration. You can inherit enumeration from IEnum interface and implement its methods. as follows

public enum Demo implements IEnum<Integer>{
        TEST1(1,"I am a"),
        TEST2(2,"I'm two"),
        TEST3(3,"I'm three"),
        TEST4(4,"I'm four"),
        TEST5(5,"I'm five"),
        TEST6(6,"I'm six")
        ;
        Demo(int code,String name){
            this.code = code;
            this.name = name;
        }
        public int code;
        private String name;

        @Override   //Implementation method
        public Integer getValue() {
            return this.code;
        }
    }

​ The incoming generic is the type value stored in the database. The enumeration value is obtained through the implementation method and compared with the value in the database.

​ Compared with the native mybatis, it may be much simpler. Mybatis Xu Tiao knows about the typeHandle interface. I'm not interested in explaining it in detail here this way please.

Spring enumeration transformation

​ As mentioned earlier, the Controller layer input parameter Jackson cannot accept enumerations in the form, which can be solved through Convert. This method is relatively troublesome, but it can solve many parameter conversion problems.

​ The specific operations are as follows

@Component
public class MyConverter implements ConverterFactory<String, ConstantUtils.Demo> {
    @Override
    public <T extends ConstantUtils.Demo> Converter<String, T> getConverter(Class<T> aClass) {
        return source -> {
            for (T enumConstant : aClass.getEnumConstants()) {
                boolean b = enumConstant.getCode() == Integer.valueOf(source);
                System.out.println(b);
                if (b)
                    return enumConstant;
            }
            return null;
        };
    }
}

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private MyConverter converter;

    @Override
    protected void addFormatters(FormatterRegistry registry) {
        super.addFormatters(registry);
        registry.addConverterFactory(converter);
    }
}

​ Converterfactory < string, constantutils. Demo > on the left is the web request input parameter, and on the right is the enumeration type to be converted. The method is very simple. In fact, it is also detailed with the above operations. Obtain a value and loop all enumeration values internally. If it is equal, it returns null. If it is not equal, it returns null.

​ The above is my reading materials and my previous accumulation. I hope it can help you!

​ Here are some blogs for reference.

1. https://blog.csdn.net/z69183787/article/details/54292789/

2. https://blog.csdn.net/weixin_38560512/article/details/115583234
3. https://blog.csdn.net/chenlu4447/article/details/100626415

Posted by TGM on Thu, 28 Oct 2021 08:04:30 -0700