Skip to content

FileUpload is missing in a MVC controller #2570

@extbe

Description

@extbe

I have the following Document entity:

import io.jooby.FileUpload;

public class Document {
    private String name;
    private FileUpload file;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public FileUpload getFile() {
        return file;
    }

    public void setFile(FileUpload file) {
        this.file = file;
    }
}

Controller:

import io.jooby.annotations.Consumes;
import io.jooby.annotations.FormParam;
import io.jooby.annotations.POST;
import io.jooby.annotations.Path;

@Path("/")
public class Controller {

    @POST("/documents")
    @Consumes("multipart/form-data")
    public void uploadTest(@FormParam Document document) {
        System.out.println("name: " + document.getName());
        System.out.println("file: " + document.getFile().getFileName());
    }
}

I try to upload the form:

curl --request POST 'localhost:8080/documents' --form 'name="My Document"' --form file=@"any-file-here"

And in logs I have an error:

name: My Document
[2022-04-28 12:08:04,300]-[worker task-2] ERROR app.App - POST /documents 500 Server Error
java.lang.NullPointerException: Cannot invoke "io.jooby.FileUpload.getFileName()" because the return value of "app.Document.getFile()" is null
	at app.Controller.uploadTest(Controller.java:15)
	at app.Controller$Module.postUploadTest$document(app.Controller$Module.java)
	at io.jooby.Route$Before.lambda$then$e67b40fd$1(Route.java:134)
	at io.jooby.internal.handler.SendDirect.apply(SendDirect.java:22)
	at io.jooby.internal.handler.WorkerHandler.lambda$apply$0(WorkerHandler.java:23)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1348)
	at org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1282)
	at java.base/java.lang.Thread.run(Thread.java:833)

The name field is set correctly but the file is missing. A workaround I see is to use an 'all-args-constructor'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions