elcadro
elcadro

Reputation: 1492

NoClassDefFoundError: mapping files from CommonsMultipartFile to byte[]

I'm trying to map with dozer from CommonsMultipartFile to byte[].

I know I need a customConverter cause dozer doesn't know anything about CommonsMultipartFile type, so I made it:

    public class FileJtfConverter extends DozerConverter<CommonsMultipartFile, byte[]> {

    /**
     * Constructor
     */
    public FileJtfConverter() {
        super(CommonsMultipartFile.class, byte[].class);
    }

    @Override
    public final byte[] convertTo(CommonsMultipartFile a, byte[] b) {
        if (a != null) {
            return a.getBytes();
        }
        return null;
    }

    @Override
    public final CommonsMultipartFile convertFrom(byte[] b, CommonsMultipartFile a) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    }

And my dozer xml file:

<mapping type="one-way">        
    <class-a>myPackage.ClassA
    </class-a>
    <class-b>myPackage.ClassB
    </class-b>
    ...
    <field custom-converter="es.xunta.formacion.sifo3.transporte.util.converter.FileJtfConverter">
       <a>anexo</a>
       <b>anexo</b>
    </field>
</mapping>

Where Class A and class B are:

public class ClassA{
    ...
    private CommonsMultipartFile anexo;
    ...
    public final CommonsMultipartFile getAnexo() {
    return anexo;
    }

    public final void setAnexo(CommonsMultipartFile anexo) {
    this.anexo = anexo;
    }
}

public class ClassB{
    ...
    protected byte[] anexo;
    ...
    public void setAnexo(byte[] value) {
    this.anexo = ((byte[]) value);
    }

    public byte[] getAnexoPago() {
        return anexoPago;
    }
}

All seems ok, but it's throwing an exception: org.dozer.MappingException: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException

And this is pretty weird, because I have defined the dependencies in my pom.xml file...

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>                
    </dependency>   

Any ideas..? Thanks a lot!

Upvotes: 0

Views: 2035

Answers (1)

adarshr
adarshr

Reputation: 62603

If you're using Spring MVC, you could just register a CustomEditor in your method annotated with @InitBinder.

There is a ByteArrayMultipartFileEditor already available that can do automatic conversion from MultipartFile to byte[] for you.

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
}

Your domain object / form can directly hold byte[] instead of MultipartFile.

I believe you could do the same with Spring Portlet MVC as well.

Upvotes: 3

Related Questions