SpringBoot(十七):SpringBoot2.1.1數據類型轉化器Converter

什麼場景下須要使用類型化器Converter?

springboot2.1.1在作Restful Api開發過程當中每每但願接口直接接收date類型參數,可是默認不加設置是不支持的,會拋出異常:系統是但願接收date類型,string沒法轉化爲date錯誤。前端

{
  "timestamp": "2019-10-29 11:52:05",
  "status": 400,
  "error": "Bad Request",
  "message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; 
nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value '2019-10-09';
nested exception is java.lang.IllegalArgumentException", "path": "/api/v1/articles" }

此時就須要配置自定義類型轉化器。java

實際上在SpringMvc框架中已經內置了不少類型轉化器,當發送一個post,get等請求後,調用請求方法以前會對方法參數進行類型轉化,默認在SpringMvc系統中由‘org.springframework.core.convert.support.DefaultConversionService’裝配了一套默認Converters。web

public class DefaultConversionService extends GenericConversionService {

    @Nullable
    private static volatile DefaultConversionService sharedInstance;


    /**
     * Create a new {@code DefaultConversionService} with the set of
     * {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}.
     */
    public DefaultConversionService() {
        addDefaultConverters(this);
    }


    /**
     * Return a shared default {@code ConversionService} instance,
     * lazily building it once needed.
     * <p><b>NOTE:</b> We highly recommend constructing individual
     * {@code ConversionService} instances for customization purposes.
     * This accessor is only meant as a fallback for code paths which
     * need simple type coercion but cannot access a longer-lived
     * {@code ConversionService} instance any other way.
     * @return the shared {@code ConversionService} instance (never {@code null})
     * @since 4.3.5
     */
    public static ConversionService getSharedInstance() {
        DefaultConversionService cs = sharedInstance;
        if (cs == null) {
            synchronized (DefaultConversionService.class) {
                cs = sharedInstance;
                if (cs == null) {
                    cs = new DefaultConversionService();
                    sharedInstance = cs;
                }
            }
        }
        return cs;
    }

    /**
     * Add converters appropriate for most environments.
     * @param converterRegistry the registry of converters to add to
     * (must also be castable to ConversionService, e.g. being a {@link ConfigurableConversionService})
     * @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService
     */
    public static void addDefaultConverters(ConverterRegistry converterRegistry) {
        addScalarConverters(converterRegistry);
        addCollectionConverters(converterRegistry);

        converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));
        converterRegistry.addConverter(new StringToTimeZoneConverter());
        converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());
        converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());

        converterRegistry.addConverter(new ObjectToObjectConverter());
        converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));
        converterRegistry.addConverter(new FallbackObjectToStringConverter());
        converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry));
    }

    /**
     * Add common collection converters.
     * @param converterRegistry the registry of converters to add to
     * (must also be castable to ConversionService, e.g. being a {@link ConfigurableConversionService})
     * @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService
     * @since 4.2.3
     */
    public static void addCollectionConverters(ConverterRegistry converterRegistry) {
        ConversionService conversionService = (ConversionService) converterRegistry;

        converterRegistry.addConverter(new ArrayToCollectionConverter(conversionService));
        converterRegistry.addConverter(new CollectionToArrayConverter(conversionService));

        converterRegistry.addConverter(new ArrayToArrayConverter(conversionService));
        converterRegistry.addConverter(new CollectionToCollectionConverter(conversionService));
        converterRegistry.addConverter(new MapToMapConverter(conversionService));

        converterRegistry.addConverter(new ArrayToStringConverter(conversionService));
        converterRegistry.addConverter(new StringToArrayConverter(conversionService));

        converterRegistry.addConverter(new ArrayToObjectConverter(conversionService));
        converterRegistry.addConverter(new ObjectToArrayConverter(conversionService));

        converterRegistry.addConverter(new CollectionToStringConverter(conversionService));
        converterRegistry.addConverter(new StringToCollectionConverter(conversionService));

        converterRegistry.addConverter(new CollectionToObjectConverter(conversionService));
        converterRegistry.addConverter(new ObjectToCollectionConverter(conversionService));

        converterRegistry.addConverter(new StreamConverter(conversionService));
    }

    private static void addScalarConverters(ConverterRegistry converterRegistry) {
        converterRegistry.addConverterFactory(new NumberToNumberConverterFactory());

        converterRegistry.addConverterFactory(new StringToNumberConverterFactory());
        converterRegistry.addConverter(Number.class, String.class, new ObjectToStringConverter());

        converterRegistry.addConverter(new StringToCharacterConverter());
        converterRegistry.addConverter(Character.class, String.class, new ObjectToStringConverter());

        converterRegistry.addConverter(new NumberToCharacterConverter());
        converterRegistry.addConverterFactory(new CharacterToNumberFactory());

        converterRegistry.addConverter(new StringToBooleanConverter());
        converterRegistry.addConverter(Boolean.class, String.class, new ObjectToStringConverter());

        converterRegistry.addConverterFactory(new StringToEnumConverterFactory());
        converterRegistry.addConverter(new EnumToStringConverter((ConversionService) converterRegistry));

        converterRegistry.addConverterFactory(new IntegerToEnumConverterFactory());
        converterRegistry.addConverter(new EnumToIntegerConverter((ConversionService) converterRegistry));

        converterRegistry.addConverter(new StringToLocaleConverter());
        converterRegistry.addConverter(Locale.class, String.class, new ObjectToStringConverter());

        converterRegistry.addConverter(new StringToCharsetConverter());
        converterRegistry.addConverter(Charset.class, String.class, new ObjectToStringConverter());

        converterRegistry.addConverter(new StringToCurrencyConverter());
        converterRegistry.addConverter(Currency.class, String.class, new ObjectToStringConverter());

        converterRegistry.addConverter(new StringToPropertiesConverter());
        converterRegistry.addConverter(new PropertiesToStringConverter());

        converterRegistry.addConverter(new StringToUUIDConverter());
        converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter());
    }

}

DefaultConversionService中在給converterRegistry添加轉化器分爲了三類去添加:addScalarConverters-參數到其餘類型參數;addCollectionConverters-集合轉化器;addDefaultConverters-默認轉化器。spring

查找相應類型轉化器的方式,經過sourceType,targetType去配置。在註冊轉化器時,會記錄該converter是將什麼類型的數據處理爲何類型的數據,其實就是記錄了sourceType,targetType。json

SpringMvc中Converter的用法

Converter是SpringMvc框架中的一個功能點,經過轉化器能夠實現對UI端傳遞的數據進行類型轉化,實現類型轉化能夠實現接口Converter<S,T>接口、ConverterFactory接口、GenericConverter接口。ConverterRegistry接口就是對這三種類型提供了對應的註冊方法。後端

Converter接口用法:

Converter接口的定義:api

public interface Converter<S, T> {
    T convert(S source);
}

接口是使用了泛型的,第一個類型表示原類型,第二個類型表示目標類型,而後裏面定義了一個convert方法,將原類型對象做爲參數傳入進行轉換以後返回目標類型對象。
用法:springboot

自定義實現字符串日期轉化爲日期類型供接口接收:服務器

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {
    private static ThreadLocal<SimpleDateFormat[]> formats = new ThreadLocal<SimpleDateFormat[]>() {
        protected SimpleDateFormat[] initialValue() {
            return new SimpleDateFormat[]{
                    new SimpleDateFormat("yyyy-MM"),
                    new SimpleDateFormat("yyyy-MM-dd"),
                    new SimpleDateFormat("yyyy-MM-dd HH"),
                    new SimpleDateFormat("yyyy-MM-dd HH:mm"),
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            };
        }
    };

    @Override
    public Date convert(String source) {
        if (source == null || source.trim().equals("")) {
            return null;
        }

        Date result = null;
        String originalValue = source.trim();
        if (source.matches("^\\d{4}-\\d{1,2}$")) {
            return parseDate(source, formats.get()[0]);
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
            return parseDate(source, formats.get()[1]);
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}$")) {
            return parseDate(source, formats.get()[2]);
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formats.get()[3]);
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formats.get()[4]);
        } else if (originalValue.matches("^\\d{1,13}$")) {
            try {
                long timeStamp = Long.parseLong(originalValue);
                if (originalValue.length() > 10) {
                    result = new Date(timeStamp);
                } else {
                    result = new Date(1000L * timeStamp);
                }
            } catch (Exception e) {
                result = null;
                e.printStackTrace();
            }
        } else {
            result = null;
        }

        return result;
    }

    /**
     * 格式化日期
     *
     * @param dateStr    String 字符型日期
     * @param dateFormat 日期格式化器
     * @return Date 日期
     */
    public Date parseDate(String dateStr, DateFormat dateFormat) {
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (Exception e) {

        }
        return date;
    }
}

在WebMvcConfiguration中注入該Converter.app

/**
 * WebMvcConfigurerAdapter 這個類在SpringBoot2.0已過期,官方推薦直接實現 WebMvcConfigurer 這個接口
 */
@Configuration
@Import({WebMvcAutoConfiguration.class})
@ComponentScan(
        value = "com.dx.test.web",
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)
        })
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public StringToDateConverter stringToDateConverter() {
        return new StringToDateConverter();
    }
    ...
}

這樣前端訪問Restful api時,當api接口,接口須要接收date類型的參數時,前端傳入日期字符串後,後端會使用該類型轉化器將參數轉化爲date後傳遞給api接口函數。

考慮這樣一種狀況,咱們有一個表示用戶狀態的枚舉類型UserStatus,若是要定義一個從String轉爲UserStatus的Converter,根據以前Converter接口的說明,咱們的StringToUserStatus大概是這個樣子:

public class StringToUserStatus implements Converter<String, UserStatus> {  
   @Override  
   public UserStatus convert(String source) {  
       if (source == null) {  
          return null;  
       }  
       return UserStatus.valueOf(source);  
   }   
}  

若是這個時候有另一個枚舉類型UserType,那麼咱們就須要定義另一個從String轉爲UserType的Converter——StringToUserType,那麼咱們的StringToUserType大概是這個樣子:

public class StringToUserType implements Converter<String, UserType> {  
   @Override  
   public UserType convert(String source) {  
       if (source == null) {  
          return null;  
       }  
       return UserType.valueOf(source);  
   }   
}  

若是還有其餘枚舉類型須要定義原類型爲String的Converter的時候,咱們還得像上面那樣定義對應的Converter。有了ConverterFactory以後,這一切都變得很是簡單,由於UserStatus、UserType等其餘枚舉類型同屬於枚舉,因此這個時候咱們就能夠統必定義一個從String到Enum的ConverterFactory,而後從中獲取對應的Converter進行convert操做。

ConverterFactory接口的用法:

ConverterFactory接口的定義:

public interface ConverterFactory<S, R> { 
    <T extends R> Converter<S, T> getConverter(Class<T> targetType);
}

用法:

Spring官方已經爲咱們實現了這麼一個StringToEnumConverterFactory:

Spring官方已經爲咱們實現了這麼一個StringToEnumConverterFactory:
package org.springframework.core.convert.support

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

@SuppressWarnings({"unchecked", "rawtypes"})
final class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {  
   
    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {  
       return new StringToEnum(targetType);  
    }  
   
    private class StringToEnum<T extends Enum> implements Converter<String, T> {  
   
       private final Class<T> enumType;  
   
       public StringToEnum(Class<T> enumType) {  
           this.enumType = enumType;  
       }  
   
       public T convert(String source) {  
           if (source.length() == 0) {  
              // It's an empty enum identifier: reset the enum value to null.  
              return null;  
           }  
           return (T) Enum.valueOf(this.enumType, source.trim());  
       }  
    }  
   
}

GenericConverter接口的用法:

GenericConverter接口是全部的Converter接口中最靈活也是最複雜的一個類型轉換接口。

Converter接口只支持從一個原類型轉換爲一個目標類型;ConverterFactory接口只支持從一個原類型轉換爲一個目標類型對應的子類型;而GenericConverter接口支持在多個不一樣的原類型和目標類型之間進行轉換,這也就是GenericConverter接口靈活和複雜的地方。

GenericConverter接口的定義:

public interface GenericConverter {  
     
    Set<ConvertiblePair> getConvertibleTypes();  
   
    Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);  
   
    public static final class ConvertiblePair {  
   
       private final Class<?> sourceType;  
   
       private final Class<?> targetType;  
   
       public ConvertiblePair(Class<?> sourceType, Class<?> targetType) {  
           Assert.notNull(sourceType, "Source type must not be null");  
           Assert.notNull(targetType, "Target type must not be null");  
           this.sourceType = sourceType;  
           this.targetType = targetType;  
       }  
   
       public Class<?> getSourceType() {  
           return this.sourceType;  
       }  
   
       public Class<?> getTargetType() {  
           return this.targetType;  
       }  
    }  
}

關於GenericConverter的使用,這裏也舉一個例子。假設咱們有一項需求是但願能經過user的id或者username直接轉換爲對應的user對象,那麼咱們就能夠針對於id和username來創建一個GenericConverter。這裏假設id是int型,而username是String型的,因此咱們的GenericConverter能夠這樣來寫:

public class UserGenericConverter implements GenericConverter {  
   
    @Autowired  
    private UserService userService;  
     
    @Override  
    public Object convert(Object source, TypeDescriptor sourceType,  
           TypeDescriptor targetType) {  
       if (source == null || sourceType == TypeDescriptor.NULL || targetType == TypeDescriptor.NULL) {  
           return null;  
       }  
       User user = null;  
       if (sourceType.getType() == Integer.class) {  
           user = userService.findById((Integer) source);//根據id來查找user  
       } else if (sourceType.getType() == String.class) {  
           user = userService.find((String)source);//根據用戶名來查找user  
       }  
       return user;  
    }  
   
    @Override  
    public Set<ConvertiblePair> getConvertibleTypes() {  
       Set<ConvertiblePair> pairs = new HashSet<ConvertiblePair>();  
       pairs.add(new ConvertiblePair(Integer.class, User.class));  
       pairs.add(new ConvertiblePair(String.class, User.class));  
       return pairs;  
    }  
}  

使用GenericConverter實現對@RequestHeader中文參數值進行解碼

默認從UI端傳入到服務器端的header中文參數都會被encoder,爲了實現對header中文解碼,能夠經過GenericConverter實現解碼。

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestHeader;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.Set;

public class RequestHeaderDecodeConverter implements GenericConverter {
    private static final String ENCODE = "utf-8";
    private String encoder = null;

    public RequestHeaderDecodeConverter(@Nullable String encoder) {
        if (encoder == null) {
            this.encoder = ENCODE;
        } else {
            this.encoder = encoder;
        }
    }

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        Set<ConvertiblePair> pairs = new HashSet<ConvertiblePair>();
        pairs.add(new ConvertiblePair(String.class, String.class));
        return pairs;
    }

    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (source == null || sourceType == null || targetType == null) {
            return null;
        }

        Object userName = source;
        if (targetType.hasAnnotation(RequestHeader.class) && targetType.getType().equals(String.class)) {
            try {
                System.out.println(source.toString());
                userName = (source != null ? URLDecoder.decode(source.toString(), ENCODE) : null);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return userName;
    }
}

在SpringBoot中配置中引入

/**
 * WebMvcConfigurerAdapter 這個類在SpringBoot2.0已過期,官方推薦直接實現 WebMvcConfigurer 這個接口
 */
@Configuration
@Import({WebMvcAutoConfiguration.class})
@ComponentScan(
        value = "com.dx.test.web",
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)
        })
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public RequestHeaderDecodeConverter requestHeaderDecodeConverter() {
        return new RequestHeaderDecodeConverter(null);
    }
    ...
}

測試Controller接口

    @ApiOperation(value = "查詢文章列表", code = 200, httpMethod = "GET", produces = "application/json", notes = "queryById方法定義說明:根據title檢索文章,返回文章列表。")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", paramType = "header", value = "操做用戶id", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userName", paramType = "header", value = "操做用戶", required = false, dataType = "String"),
            @ApiImplicitParam(name = "title", paramType = "query", value = "文章標題檢索值", required = false, dataType = "String"),
            @ApiImplicitParam(name = "articleType", paramType = "query", value = "文章類型", required = false, dataType = "ArticleType"),
            @ApiImplicitParam(name = "createTime", paramType = "query", value = "文章發佈時間", required = false, dataType = "Date")
    })
    @RequestMapping(value = {"/articles"}, method = {RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public List<Article> queryList(
            @RequestHeader(value = "userId", required = false) String userId,
            @RequestHeader(value = "userName", required = false) String userName,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "articleType",required = false) ArticleType articleType,
            @RequestParam(value = "createTime", required = false) Date createTime) {
        System.out.println(createTime);
        List<Article> articles = new ArrayList<>();
        articles.add(new Article(1L, "文章1", "", "", new Date()));
        articles.add(new Article(2L, "文章2", "", "", new Date()));
        articles.add(new Article(3L, "文章3", "", "", new Date()));
        articles.add(new Article(4L, "文章4", "", "", new Date()));

        return articles.stream().filter(s -> s.getTitle().contains(title)).collect(Collectors.toList());
    }

斷點在Resetful api內部,能夠發現當WebMvcConfiguration中注入 RequestHeaderDecodeConverter 對userName是否encoder變化狀況。

參考:《SpringMVC之類型轉換Converter》 

SpringMVC數據類型轉換——第七章 註解式控制器的數據驗證、類型轉換及格式化——跟着開濤學SpringMVC

相關文章
相關標籤/搜索