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!");
}
}
精彩评论