-1

My frontend server sends a json built from a VueJS form. I am trying to get, to map and to parse this json using Jersey 2.32.

{
    "objetDesLiensDuTheme": {
        "id": 7195,
        "formalisation": {
            "selectTitreFichier": "fiche de formalisation",
            "inputTitreFichier": "fiche de formalisation titre fichier",
            "inputFichier": {},
        },
        "modop": {
            "selectTitreFichier": "fiche méthodologique",
            "inputTitreFichier": "fiche méthodologique titre fichier",
            "inputFichier": {},
        },
    }
}

I've stringyfied it to show its structure here but it is a JSON object and a console.log shows me also the content of "inputFichier":

inputFichier: File { name: "Fichier formalisation.txt", lastModified: 1690373320586, size: 0, type: "text/plain", webkitRelativePath: ""}

My REST controller method looks like this:

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
public Response createLien(@BeanParam LiensDTO objetDesLiensDuTheme)
{
    Response response;
    //TODO
    System.out.println(objetDesLiensDuTheme);

    response = Response.status(Status.CREATED).build();
    return response;
}

But according to the toString method, the bean parameter seems to be null:

LiensDTO [objetDesLiensDuTheme=null]

Here is the LiensDTO class (getter and setter not shown):

    public class LiensDTO implements Serializable
    {
        private static final long serialVersionUID = -5717326975468155988L;

        public LiensDTO()
        {
            super();
        }

        @FormParam("objetDesLiensDuTheme")
        // @JsonProperty("objetDesLiensDuTheme")
        private DocLienDTO objetDesLiensDuTheme;

        @Override
        public String toString()
        {
            return "LiensDTO [objetDesLiensDuTheme=" + objetDesLiensDuTheme + "]";
        }
    }

Here is the DocLienDTO class (getters and setters not shown):

    public class DocLienDTO implements Serializable
    {
        private static final long serialVersionUID = -493282593738066056L;

        private int id;

        private DocumentDTO formalisation;

        private DocumentDTO modop;
    }

And finally the DocumentDTO class (getters and setters not shown):

public class DocumentDTO implements Serializable
{
  private static final long serialVersionUID = -6698584238714969958L;

  private String selectTitreFichier;

  private String inputTitreFichier;

  private File inputFichier;

}

When I try without the @BeanParam annotation (like described here or with the @RequestBody annotation in the rest controller, I get this exception: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofapi.bean.LiensDTOout of START_ARRAY token at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 1].

However, the json object doesn't contain arrays.

I also tried with @JsonProperty("objetDesLiensDuTheme") instead of @FormParam("objetDesLiensDuTheme").

I would like to get and to iterate through the "objetDesLiensDuTheme" object for my upload module.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
LBS
  • 518
  • 8
  • 17
  • You should take a look at the actual JSON you are sending out from the browser, and the raw JSON string that is coming in. If Jackson tells you there's a start array token at line 1, column 1, that's what it is. – Jorn Aug 01 '23 at 06:40

1 Answers1

0

The problem was the fact that my JSON contained File objects.

But it is not possible to directly convert a File object into JSON, even using JSON.stringify.

There are three solutions described here. I chose to base64 encode the file before stringify the whole body object.

LBS
  • 518
  • 8
  • 17