2011-04-24 34 views
3

我是新來的推土機,我試圖將一個字符串映射到一個布爾值,反之亦然。任何人都可以告訴我Dozer是否支持這個功能,或者我是否必須創建一個自定義轉換器。該字符串將包含true或false,因此將直接映射。另外我使用Dozer API而不是XML配置。感謝您的幫助如何使用推土機將布爾轉換爲字符串?

回答

3

我不認爲dozer支持開箱即用,您可以使用自定義轉換器爲您完成這項工作。事實上在custom converters幫助頁面使用此情況下,例如:

public class NewDozerConverter extends DozerConverter<String, Boolean> { 

    public NewDozerConverter() { 
    super(String.class, Boolean.class); 
    } 

    public Boolean convertTo(String source, Boolean destination) { 
    if ("true".equals(source)) { 
     return Boolean.TRUE; 
    } else if ("false".equals(source)) { 
     return Boolean.FALSE; 
    } 
    throw new IllegalStateException("Unknown value!"); 
    } 

    public String convertFrom(Boolean source, String destination) { 
    if (Boolean.TRUE.equals(source)) { 
     return "true"; 
    } else if (Boolean.FALSE.equals(source)) { 
     return "false"; 
    } 
    throw new IllegalStateException("Unknown value!"); 
    } 

} 
+0

這工作謝謝 – irishguy 2013-09-17 09:53:46

1

我相信(我的頭頂部),其推土機支持,因爲它委託給BeanUtils的這種轉換。我知道ModelMapper不支持布爾到字符串,反之亦然。