Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
61stAirhacksQ&A.md

Ask questions and see you at April, 6th, 6.PM. CET: http://www.ustream.tv/channel/adambien

Also checkout recent episode:

60th airhacks.tv

Please keep the questions Java EE-stic. Means: as short and as concise as only possible. Feel free to ask several, shorter questions.

@nkengasongatem

This comment has been minimized.

Copy link

nkengasongatem commented Mar 16, 2019

Hi Adam, I am working on project which uses JS at the frontend and Jakarta EE at the backend. I would like to persist submitted form data containing a picture, and save this picture as blob (despite the shortcomings) in a MySQL database. I tried the method below but JSONB fails during deserialization of the 'candidatePicture' field.

 var file = URL.createObjectURL(document.getElementById("candidatePicture").files[0]);
         fetch(file).then(response => {
            response.blob().then(photo => {

             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) => {
                
                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) => {
                    if (jqXHR.status === 200) {
                        swal("The ballot has been succesfully created!", {icon: "success"})
                                .then((confirm) => {
                                    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) => {
                    swal("Error! An error occured!", {icon: "error"});
                });

            });

        });
    });

In my netbeans 8.2 console i get:
Severe: javax.ws.rs.ProcessingException: Error deserializing object from entity stream. javax.ws.rs.ProcessingException: Error deserializing object from entity stream.

This is my resource endpoint method:

  @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) {

    Election election = electionService.find(Long.parseLong(electionid));
    newBallot.setElectionID(election);
    ballotsService.create(newBallot); 
    return Response.ok(newBallot).build();
 }

Please how can i get this to work? Thanks in advance

@AdamBien

This comment has been minimized.

Copy link
Owner Author

AdamBien commented Mar 20, 2019

Thoughts on quarkus.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.