-2

I'm having a problem when uploading a file using MultipartFile. I'm using Spring v2.2.6.RELEASE whenever i upload i get the value string but the file is NULL. Here's my code below

UploadController.java

 @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response upload(@BeanParam AttachmentRequestDTO attachmentRequestDTO) {
        log.info("Hello - from upload endpoint");
        log.info("senderId: "+ attachmentRequestDTO.getSenderId());
        log.info("groupId: "+ attachmentRequestDTO.getGroupId());
        log.info("file: "+ attachmentRequestDTO.getFile().isEmpty());
        return Response.status(200).entity("test123").build();
    
    }

AttachmentRequestDTO

public class AttachmentRequestDTO {

    @FormDataParam("file")
    private MultipartFile file;

    @FormDataParam("senderId")
    private String senderId;

    @FormDataParam("groupId")
    private String groupId;

...

Config.java

@Configuration
public class Config {

    @Bean(name="multipartResolver")
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("utf-8");
        commonsMultipartResolver.setMaxUploadSize(20000000);
        commonsMultipartResolver.setResolveLazily(false);
        return commonsMultipartResolver;
    }
}

SpringApplication.java

@SpringBootApplication
public class AttachmentApiApplication extends BaseMerakApplication {

    public static void main(String[] args) {
        SpringApplication.run(AttachmentApiApplication.class, args);
    }

    @Bean
    protected Consumer<ResourceConfig> customJerseyConfig() {
        return resourceConfig -> {
            resourceConfig.register(MultiPartFeature.class);
            resourceConfig.register(Config.class);
        };
    }

    @Bean
    public RequestContextFilter requestContextFilter() {
        return new RequestContextFilter();
    }
}

Here's the screenshot when hitting on postman Postman form-data

And here's the screenshot result for the log file. as you see I can get the value for the string but the file I'm getting is always null. I tried to add the multipartResolver on the config file as this resolves their problem but for me, I'm not getting the same result. Does anyone encountered this issue before and how did you solve this. Is there specific version of jersey to be used for this?

2020-07-21 12:42:19.774  INFO 17683 --- [nio-8092-exec-1] c.i.c.a.a.p.FileUploadController         : Hello - from upload endpoint
2020-07-21 12:42:19.774  INFO 17683 --- [nio-8092-exec-1] c.i.c.a.a.p.FileUploadController         : senderId: testCjid123
2020-07-21 12:42:19.774  INFO 17683 --- [nio-8092-exec-1] c.i.c.a.a.p.FileUploadController         : groupId: ChannelId123
2020-07-21 12:42:19.779 ERROR 17683 --- [nio-8092-exec-1] c.i.a.t.s.f.ThrowableProcessingFilter    : An unexpected exception occurred while handling the request
javax.servlet.ServletException: java.lang.NullPointerException
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:410) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.LocalDtabFilter.$anonfun$doFilter$6(LocalDtabFilter.scala:48) ~[toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.LocalDtabFilter$$Lambda$1204.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at com.twitter.finagle.DtabCompanionBase.unwind(DtabBase.scala:338) ~[finagle-core_2.12-20.4.0.jar:20.4.0]
        at com.ing.apisdk.toolkit.servlet.filter.LocalDtabFilter.doFilter(LocalDtabFilter.scala:46) ~[toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.$anonfun$callChain$1(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter$$Lambda$1166.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at scala.util.Try$.apply(Try.scala:213) ~[scala-library-2.12.11.jar:na]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.callChain(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.tracing.servlet.TracingServletFilter.$anonfun$doFilter$1(TracingServletFilter.scala:61) ~[toolkit-tracing-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.tracing.servlet.TracingServletFilter$$Lambda$1190.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at com.twitter.util.Local.let(Local.scala:4978) ~[util-core_2.12-20.4.0.jar:20.4.0]
        at com.twitter.finagle.context.LocalContext.letLocal(LocalContext.scala:51) ~[finagle-core_2.12-20.4.0.jar:20.4.0]
        at com.twitter.finagle.context.LocalContext.let(LocalContext.scala:24) ~[finagle-core_2.12-20.4.0.jar:20.4.0]
        at com.ing.apisdk.toolkit.connectivity.tracing.core.OpenTracingContext$.withParentSpan(OpenTracingContext.scala:20) ~[toolkit-connectivity-tracing-core_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.tracing.servlet.TracingServletFilter.doFilter(TracingServletFilter.scala:58) ~[toolkit-tracing-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.$anonfun$callChain$1(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter$$Lambda$1166.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at scala.util.Try$.apply(Try.scala:213) ~[scala-library-2.12.11.jar:na]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.callChain(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.doFilter(AsyncAwareHttpServletFilter.scala:52) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.$anonfun$callChain$1(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter$$Lambda$1166.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at scala.util.Try$.apply(Try.scala:213) ~[scala-library-2.12.11.jar:na]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.callChain(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.doFilter(AsyncAwareHttpServletFilter.scala:52) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_232]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_232]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at java.lang.Thread.run(Thread.java:819) [na:1.8.0_232]
Caused by: java.lang.NullPointerException: null
        at com.ing.contacting.attachments.api.presentation.FileUploadController.upload(FileUploadController.java:46) ~[classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_232]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_232]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_232]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_232]
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$$Lambda$1272.0000000000000000.invoke(Unknown Source) ~[na:na]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:244) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394) ~[jersey-container-servlet-core-2.30.1.jar:na]
        ... 72 common frames omitted

also whenever start this, a log warning tells me that the ConfigSetup.class is ignored. I'm not sure if this is one of the reasons why I'm having NULL pointer.

2020-07-21 21:02:29.352  WARN 51860 --- [           main] o.g.jersey.internal.inject.Providers     : A provider com.ing.contacting.attachments.api.settings.ConfigSetup registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.ing.contacting.attachments.api.settings.ConfigSetup will be ignored. 
  • 4
    Don't post logs and stacktraces as images. Please edit your question and correct that. – Robert Jul 20 '20 at 15:53
  • 1
    @FormDataParam("**file**") vs screenshot "**files**". You even named the *class* field `files`, but told the annotation that the *form* field is named `file`. Voting to close as typo. – Andreas Jul 20 '20 at 16:14
  • sorry @Robert i had change the logs image. – Dominic Hong Jul 21 '20 at 12:56
  • Sorry @Andreas, my bad for wrong paramater name on postman and DTO class. i had updated it to "file" and still same NULL pointer error occur – Dominic Hong Jul 21 '20 at 12:59

2 Answers2

1

The form-data part is called file in your source code, not files.

maio290
  • 6,440
  • 1
  • 21
  • 38
  • Sorry @maio290, my bad for wrong paramater name on postman and DTO class. i had updated it to "file" and still same NULL pointer error occur – Dominic Hong Jul 21 '20 at 12:59
0

Solved by using FormDataBodyPart instead of MultipartFile on AttachmentRequestDTO class.

public class AttachmentRequestDTO {


@NotNull
@FormDataParam("file")
private FormDataBodyPart file;

@NotNull
@FormDataParam("senderId")
private String senderId;

@NotNull
@FormDataParam("groupId")
private String groupId;