`/**
- abssqr.com Inc.
- Copyright (c) 2017-2018 All Rights Reserved. */
package com.abssqr.plat.convert;
import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** *
-
guozhenhua
-
com.abssqr.plat.convert: EnumConvertConfigurer.java, v 0.1 2018-03-11 下午4:14 guozhenhua Exp $ */ @Configuration public class EnumConvertConfigurer extends WebMvcConfigurerAdapter{
public void addFormatters(FormatterRegistry registry) { registry.addConverterFactory(new EnumConvertFactory()); } } `
/** * abssqr.com Inc. * Copyright (c) 2017-2018 All Rights Reserved. */package com.abssqr.plat.convert;import com.abssqr.common.xw.model.IEnum;import com.abssqr.common.xw.model.utils.IEnumUtil;import org.apache.commons.lang3.StringUtils;import org.springframework.core.convert.converter.Converter;import org.springframework.core.convert.converter.ConverterFactory;/** * * @author guozhenhua * @version com.abssqr.plat.convert: EnumConvertFactory.java, v 0.1 2018-03-11 下午4:11 guozhenhua Exp $ */public class EnumConvertFactory implements ConverterFactory{ @Override public Converter getConverter(Class targetType) { return new StringToIEum<>(targetType); } private class StringToIEum implements Converter { private Class targerType; public StringToIEum(Class targerType) { this.targerType = targerType; } @Override public T convert(String source) { if (StringUtils.isBlank(source)) { return null; } return (T)IEnumUtil.getIEnum(this.targerType,source); } }}