Jersey RESTful 映射返回 null 对象或 MismatchedInputException

问题描述 投票:0回答:1

我的前端服务器发送一个从 VueJS 表单构建的 json。 我正在尝试使用 Jersey 2.32 获取、映射和解析此 json。

{
    "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": {},
        },
    }
}

我已经将其字符串化以在此处显示其结构,但它是一个 JSON 对象,console.log 还向我显示了“inputFichier”的内容:

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

我的 REST 控制器方法如下所示:

@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;
}

但是根据toString方法来看,bean参数好像是null:

LiensDTO [objetDesLiensDuTheme=null]

这是 LiensDTO 类(未显示 getter 和 setter):

    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 + "]";
        }
    }

这是 DocLienDTO 类(未显示 getter 和 setter):

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

        private int id;

        private DocumentDTO formalisation;

        private DocumentDTO modop;
    }

最后是 DocumentDTO 类(未显示 getter 和 setter):

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

  private String selectTitreFichier;

  private String inputTitreFichier;

  private File inputFichier;

}

当我尝试不使用

@BeanParam
注释时(如此处所述) 或者使用其余控制器中的
@RequestBody
注释,我得到以下异常:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of
api.bean.LiensDTO
out of START_ARRAY token at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 1].

但是,json对象不包含数组。

我也尝试过用

@JsonProperty("objetDesLiensDuTheme")
而不是
@FormParam("objetDesLiensDuTheme")

我想获取并迭代我的上传模块的“objetDesLiensDuTheme”对象。

java json rest jersey
1个回答
0
投票

问题是我的 JSON 包含 File 对象。

但是即使使用 JSON.stringify 也不可能直接将 File 对象转换为 JSON。

此处描述了三种解决方案。我选择在对整个主体对象进行字符串化之前对文件进行 Base64 编码。

© www.soinside.com 2019 - 2024. All rights reserved.