java - How to convert URL field using Dozer bean Mapper? -


i have following classes , when used dozer bean mapper convert productentity product, , vice versa:

public class productentity(){        private string name;     private string description;     private url site; }  public class product(){     private string name;     private string description;     private url site;         } 

i following error:

internal error [java.lang.nosuchmethodexception: java.net.url.<init>()

is dozer incompatible url class, or doing wrong?

it's been while since i've done dozer, reason you're seeing due way dozer maps objects. it's looking create new object merely invoking no-arg constructor, , since url doesn't have one, why you're getting exception.

the way around create identity conversion: map 1 instance of entity exact same type of entity.

the way in 2 parts:

first, declare custom configuration in dozer.xml file.

<configuration>     <custom-converters>         <converter type="com.stackoverflow.urlconverter">             <class-a>java.net.url</class-a>             <class-b>java.net.url</class-b>         </converter>     </custom-converters> </configuration> 

next, create new urlconverter class extends dozerconverter. reason extends dozerconverter , not customconverter simplicity , type safety.

public class urlconverter extends dozerconverter<url, url> {      public urlconverter() {         super(url.class, url.class);     }      @override     public url convertto(url source, url destination) {         url result = null;         try {             result = source.touri().tourl();         } catch (malformedurlexception | urisyntaxexception e) {             throw e;         }          return result;     }      @override     public url convertfrom(url source, url destination) {         url result = null;         try {             result = source.touri().tourl();         } catch (malformedurlexception | urisyntaxexception e) {            throw e;         }          return result;     } } 

the process here mechanical:

  • attempt convert url uri, convert url.
  • if there's malformedness uri or url, throw - shouldn't dealing malformed url @ state anyway.
  • otherwise, return result of converted url.

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -