SpringBoot2.5.3自定义Converter

网上讲解基本上补出源代码,所以造成很多文章同质化严重,下面我直接贴出代码。
我按照Springboot源码中的方式改写代码,所以继承的是ConditionalGenericConverter接口,和网上不同,按照官方类修改

先编写自定义异常类StringToCollectionConverter,主要修改其中的convert方法,保证matches方法可以匹配即可

package com.wisetv.clickhouse.operation.converter;

import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

public class StringToCollectionConverter implements ConditionalGenericConverter {

    private final ConversionService conversionService;


    public StringToCollectionConverter(ConversionService conversionService) {
        this.conversionService = conversionService;
    }


    @Override
    public Set getConvertibleTypes() {
        return Collections.singleton(new ConvertiblePair(String.class, Collection.class));
    }

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        return (targetType.getElementTypeDescriptor() == null ||
                this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor()));
    }

    @Override
    @Nullable
    public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (source == null) {
            return null;
        }
        String string = (String) source;
        System.out.println("convert:"+string);
        String[] fields = StringUtils.commaDelimitedListToStringArray(string);
        TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
        Collection target = CollectionFactory.createCollection(targetType.getType(),
                (elementDesc != null ? elementDesc.getType() : null), fields.length);

        if (elementDesc == null) {
            for (String field : fields) {
                target.add(field.trim());
            }
        }
        else {
            for (String field : fields) {
                Object targetElement = this.conversionService.convert(field.trim(), sourceType, elementDesc);
                target.add(targetElement);
            }
        }
        return target;
    }

}
 
  

编写OperationWebMvcConfigurer类,将上述方法进行注册即可,此类修改addConverter中的类即可

package com.wisetv.clickhouse.operation.converter;

import org.springframework.core.convert.ConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class OperationWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        StringToCollectionConverter stringToCollectionConverter = new StringToCollectionConverter((ConversionService) registry);
        registry.addConverter(stringToCollectionConverter);
    }
}

你可能感兴趣的:(JAVA,springboot,conversion)