title: 'Error deserializing object from entity stream' after submitting a multipart/form-data using JavaScript/AJAX to JAX-RS backend
body: <p>I am developing a RESTful webapp with a JavaScript frontend and JAX-RS backend with Netbeans 8.2 so far all has been working great. I have a form where a user uploads his image and i would like to save this image as a blob in my database ( i know the shortcomings but i'd like to proceed this way). Please see the snippets below.</p>
  
  <p>First, the POST request on form submit:</p>
  
  <pre><code> var file = URL.createObjectURL(document.getElementById("candidatePicture").files[0]);
  
  
      fetch(file).then(response =&gt; {
          response.blob().then(photo =&gt; {
  
              const reader = new FileReader();
  
              // Start reading the blob as text.
              reader.readAsText(photo);
  
              // This fires after the blob has been read/loaded.
              reader.addEventListener('loadend', (e) =&gt; {
  
                  const text = e.srcElement.result;
                  var ballotData = $("#addBallotForm").serializeArray();
                  var ballot = {
                      "candidateName": ballotData[0].value,
                      "positionName": ballotData[1].value,
                      "candidateNIC": ballotData[2].value,
                      "partyname": ballotData[3].value,
                      "candidateSlogan": ballotData[4].value,
                      "candidatePicture": text,
                      "actionPlan": ""
                  };
                  console.log(JSON.stringify(ballot));
  
                  $.ajax({
                      headers: {
                          'Content-Type': 'application/json'
                      },
                      crossDomain: true,
                      type: "POST",
                      enctype: 'application/json',
                      processData: false, // Important! prevent jQuery from transforming the data into a query string 
                      cache: false,
                      url: url,
                      data: JSON.stringify(ballot)
                  }).done((data, textStatus, jqXHR) =&gt; {
                      if (jqXHR.status === 200) {
                          swal("The ballot has been succesfully created!", {icon: "success"})
                                  .then((confirm) =&gt; {
                                      if (confirm) {
                                          table.ajax.reload();
                                          clearAddBallotModal(); // clear the form
                                          $("#addBallotModal").click(); // close the modal
                                          displayBallots(); // redisplay the ballots
                                      }
                                  });
                      } else {
                          swal("Oops! Invalid ballot Input", {icon: "warning"});
                      }
                  }).fail((jqXHR, textStatus, errorThrown) =&gt; {
                      swal("Error! An error occured!", {icon: "error"});
                  });
  
              });
  
          });
      });
  </code></pre>
  
  <p>Second, the resource endpoint:</p>
  
  <pre><code> @POST
  @Path("{ipaddress}/{electionid}")
  @ApiOperation(value = "Create new ballot item", notes = "This can only be done by logged in users.")
  @ApiResponses(value = {
      @ApiResponse(code = 400, message = "Invalid ballot item Input"),
      @ApiResponse(code = 200, message = "Ballot item created")})
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public Response add(
          @ApiParam(value = "The ipaddress of the ballot item to be created", required = true) @PathParam("ipaddress") String ip,
          @ApiParam(value = "The election's id", required = true) @PathParam("electionid") String electionid,
          @ApiParam(value = "The ballot item that needs to be added", required = true) BallotItem newBallot) {
  
      Users curUser = UsersService.find(1l); 
      Election election = electionService.find(Long.parseLong(electionid));
      newBallot.setElectionID(election);
      ballotsService.create(newBallot);
      operationsService.create(new Operation(curUser, "Created a new ballot item", "Candidate name: " + newBallot.getCandidateName() + " and Election ID: " + electionid, new Date(), ip));
      return Response.ok(newBallot).build();
  }
  </code></pre>
  
  <p>I get the following error in netbeans console:</p>
  
  <pre><code>Severe:   javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
  javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
      at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:101)
      at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
      at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)
      at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
      at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:73)
      at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
      at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1093)
      at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
      at org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:271)
      at org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider$EntityValueSupplier.apply(EntityParamValueParamProvider.java:98)
      at org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider$EntityValueSupplier.apply(EntityParamValueParamProvider.java:81)
      at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.apply(ParamValueFactoryWithSource.java:75)
      at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:93)
      at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:133)
      at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:200)
      at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:704)
      at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
      at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1628)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:755)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:575)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:516)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:213)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: javax.json.bind.JsonbException: Internal error: Event START_ARRAY not found. Last data: [EVENT: START_OBJECT KEY_NAME: candidatePicture]
      at org.eclipse.yasson.internal.JsonbRiParser.moveTo(JsonbRiParser.java:203)
      at org.eclipse.yasson.internal.serializer.AbstractArrayDeserializer.moveToFirst(AbstractArrayDeserializer.java:82)
      at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserializeInternal(AbstractContainerDeserializer.java:75)
      at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
      at org.eclipse.yasson.internal.serializer.ObjectDeserializer.deserializeNext(ObjectDeserializer.java:165)
      at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserializeInternal(AbstractContainerDeserializer.java:85)
      at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
      at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:62)
      at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:52)
      at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
      at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)
      at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:99)
      ... 60 more
  </code></pre>
  
  <p>I changed <code>'application/json'</code> to <code>'mulitpart/form-data'</code> and <code>MediaType.APPLICATION_JSON</code> to other mediatypes but all i got was a <code>415</code> error in my browser console as response. Please does anyone has ideas to resolve this issue. Thanks in advance</p>