Pit 2 spring boot enumeration of use

Previous Speaking at the pit when the controller when doing parameter in the method of enumeration, the solution is equipped with a converter, then think about, if not busy every time add a comment trouble, you can add a note in front of the parameter Add a parser should be able to solve this problem.

Now on to enumerate the second pit, preconditions and on a like: is this enumeration:

@Getter 
@AllArgsConstructor 
public  enum   EnumExpenseType the implements BaseEnum { 
    small joy ( 1 ), 
    great joy ( 2 ); 

    Private  Final  int value; 
}

It now or when the parameter, as it is a complex object attribute to pass, this complex object is deserialized by jackjson get.

Is the front-end pass over a json string, we put it in the controller in anti-sequence objects.

Not demonstrate a direct say conclusions, jackson default is to use the ordinal index values ​​do maps, they still can not meet our needs.

So jackson not modify the configuration for each enumeration or make changes, add a @JsonCreator, I still prefer a unified configuration,

Directly on the code:

/ ** 
 * When the receiving solution BaseEnum distal JSON, should be handled in accordance with the value value, actual processing in accordance with the index value 
 * / 
@JsonComponent 
public  class BaseEnumDeserializer the extends JsonDeserializer <BaseEnum> { 
    @Override 
    public BaseEnum Deserialize (JP JsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException { 
        JsonNode Node = jp.getCodec () readTree (JP);. 
        String currentName = jp.currentName (); 
        Object currentValue = jp.getCurrentValue (); 
        Class findPropertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());

        BaseEnum b = EnumUtil.getEnumByValue(findPropertyType, node.intValue());

        return b;
    }
}
/**
 * @author :hkk
 */
public class AuditHttpMessageConverter extends MappingJackson2HttpMessageConverter {

    public AuditHttpMessageConverter(ObjectMapper objectMapper) {

        SimpleModule simpleModule1 = new SimpleModule();
        simpleModule1.addDeserializer(BaseEnum.class, new BaseEnumDeserializer());
        objectMapper.registerModule(simpleModule1);

        super.setObjectMapper(objectMapper);
    }
}

Then configure a webmvcconfig:

@Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //替换自带json序列化器
        MappingJackson2HttpMessageConverter converter;
        Iterator<HttpMessageConverter<?>> it = converters.iterator();
        while (it.hasNext()) {
            if (ClassUtils.isAssignable(it.next().getClass(), MappingJackson2HttpMessageConverter.class)) {
                it.remove();
            }
        }
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));//BaseEnum反序列化支持
        converters.add(new AuditHttpMessageConverter(builder.build()));
    }

Configuration is complete.

This is deserialized, and that serialization is still a problem, you can make a comment @jsonValue on value, but if trouble free, you can do a unified configuration directly on the code:

/ ** 
 * BaseEnum subclasses and, when serialized JSON, the specified sequence is getValue * / 
@JsonComponent 
public  class BaseEnumSerializer the extends JsonSerializer <BaseEnum> { 

    @Override 
    public  void the serialize (BaseEnum value, JsonGenerator Gen, SerializerProvider serializers) throws IOException { 
        gen.writeNumber (value.getValue ()); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/hankuikui/p/11429996.html