开发者

How do I convert a Boolean to String using Dozer?

I am new to Dozer and I am trying to map a String to a Boolean and vica versa. Can anyone tell me does Dozer support this or do I have to create a custom converter. The string will contain true or false so will map directly. Also I am using 开发者_StackOverflowthe Dozer API and not the XML config. Thanks for your help


I don't think dozer supports this out of the box, you can use a custom converter to do this work for you. In fact the help page on custom converters uses this case as example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜