box-java-sdk icon indicating copy to clipboard operation
box-java-sdk copied to clipboard

Upload File - java.lang.ClassCastException: class com.box.sdk.BoxAPIResponse cannot be cast to class com.box.sdk.BoxJSONResponse

Open karanbir1395 opened this issue 3 years ago • 5 comments

When I’m calling box upload api using the SDK I’m getting the following error:

java.lang.ClassCastException: class com.box.sdk.BoxAPIResponse cannot be cast to class com.box.sdk.BoxJSONResponse (com.box.sdk.BoxAPIResponse and com.box.sdk.BoxJSONResponse are in unnamed module of loader 'app')

Below is my code:

BoxFolder folder = new BoxFolder(apiConnection, request.getFolderId());
InputStream inputStream =  new BufferedInputStream(request.getFile().getInputStream());

BoxFile.Info newFileInfo = rootFolder.uploadFile(inputStream, request.getFileName());
inputStream.close();

Request.getFile is a multipartfile.

Can you please help me tell what does this error message mean and how to resolve it?

I've tried deleting and re-downloading BOX SDK multiple times from .m2/repository folder but it did not help. I've noticed that this issue also only happens in local.

I’ve refered to the following box example in order to make upload api request using sdk: https://developer.box.com/guides/uploads/direct/file/

Please let me know if anyone knows how to resolve this issue

karanbir1395 avatar Mar 21 '22 15:03 karanbir1395

Hello @karanbir1395 , can you paste full trace log? I would like to know where is this ClassCastException originating from.

antusus avatar Mar 21 '22 16:03 antusus

Hi Kamil, thanks for looking into the issue.

Below is the stacktrace of the exception:


java.lang.ClassCastException: class com.box.sdk.BoxAPIResponse cannot be cast to class com.box.sdk.BoxJSONResponse (com.box.sdk.BoxAPIResponse and com.box.sdk.BoxJSONResponse are in unnamed module of loader 'app')

                at com.box.sdk.BoxFolder.uploadFile(BoxFolder.java:569)

                at com.box.sdk.BoxFolder.uploadFile(BoxFolder.java:474)

                at com.rbc.ffg0.boxApi.services.box.BoxServiceImpl.uploadFile(BoxServiceImpl.java:38)

                at com.rbc.ffg0.boxApi.api.BoxController.uploadFile(BoxController.java:29)

                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

                at java.base/java.lang.reflect.Method.invoke(Method.java:566)

                at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)

                at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)

                at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)

                at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)

                at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)

                at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)

                at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)

                at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)

                at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)

                at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)

                at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

                at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

                at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)

                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

                at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)

                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

                at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)

                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

                at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)

                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)

                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)

                at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)

                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)

                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359)

                at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)

                at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)

                at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)

                at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735)

                at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

                at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)

                at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)

                at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

                at java.base/java.lang.Thread.run(Thread.java:829)

Debugging the code I also saw that this is the linewhere the call fails in BoxFolder.class SDK class:

response = (BoxJSONResponse)request.send();

karanbir1395 avatar Mar 21 '22 16:03 karanbir1395

Thanks, I can improve the code to handle the wrong response from API but it looks like there is an error. You said you can debug the call. Could you stop code execution in BoxFolder.java:569 and manually call request.send() and see what was returned? Usually the API returns JSON but I'm curious what did you get... If there is an error you should get a "request ID" this way I can find in the logs what was the problem and talk with the API team what happened.

And could you tell me what SDK version are you using?

antusus avatar Mar 21 '22 18:03 antusus

Hi Kamil,

To be clear response = (BoxJSONResponse)request.send(); is not line 569 in my code it is line 327. I know the stack trace says 569 but I dont think my code ever reaches line 569.

Secondly the SDK is read only so i'm not sure how can I manually call request.send(). Can you please advice and help?

karanbir1395 avatar Mar 22 '22 15:03 karanbir1395

You should be able to tell debugger to stop in any line. We are publishing source code so any modern IDE will deal with that. Even without source code, tools can now decompile Java code. Right now I have no idea what is wrong. Can you do small steps? List items in root folder, create a folder, upload some small file? Is anything working for you? Can you also tell me what SDK version are you using?

antusus avatar Mar 22 '22 16:03 antusus

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] avatar Dec 19 '22 19:12 stale[bot]

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box Java SDK and feel free to open another PR/issue at any time.

stale[bot] avatar Dec 26 '22 21:12 stale[bot]

We are facing this issue intermittently with uploading of large files. Would appreciate if anyone from the box team could point to a resolution.

arajalali avatar Jul 18 '23 11:07 arajalali