杰克逊错误解析器尝试解析子类型,但我的课程是简单的POJO

时间:2018-10-04 17:23:56

标签: java spring jackson-databind

信息错误:

{
    "@class": "java.util.LinkedHashMap",
    "timestamp": [
        "java.util.Date",
        "2018-10-04T17:12:35.742+0000"
    ],
    "status": 400,
    "error": "Bad Request",
    "message": "JSON parse error: Missing type id when trying to resolve subtype of [simple type, class br.com.adtsys.ciee.auth.user.data.request.LoginRequest]: missing type id property '@class'; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class br.com.adtsys.ciee.auth.user.data.request.LoginRequest]: missing type id property '@class'\n at [Source: (PushbackInputStream); line: 4, column: 1]",
    "path": "/auth/login"
}

但是我的课是一个简单的POJO

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class LoginRequest implements Serializable {

    private String username;
    private String password;
}

该对象是控制器中的一个参数,我在该方法上对POST方法进行请求,并使用@Body注释对该对象进行注释,并在我调用其余端点spring时尝试在我的对象中转换JSON,但是错误发生。我正在使用嵌入在Spring Boot版本2.0.5中的杰克逊库,该错误在我将Spring Boot版本1.5.3升级到2.0.5之后开始发生。 我已经搜索了这个问题,但不幸的是,到目前为止没有任何搜索。 有人对此问题有所了解。

1 个答案:

答案 0 :(得分:0)

经过大量研究,我发现,我需要在json中发送一个属性名称为@class的属性,而该属性值需要编写类名称和包路径。如果我发送该JSON,就可以了。

{
  "@class": "com.mypackage.LoginRequest",
  "username": "000001",
  "password": "adminciee2017"
}

但是,如果我发送JSON,则会发出@class属性名称和值

{
  "username": "000001",
  "password": "adminciee2017"
}

Jackson无法转换我的JSON并引发该响应的InvalidTypeIdException Returnig

{
"@class": "java.util.LinkedHashMap",
"timestamp": [
    "java.util.Date",
    "2018-10-04T20:41:56.369+0000"
],
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Missing type id when trying to resolve subtype of [simple type, class br.com.adtsys.ciee.auth.user.data.request.LoginRequest]: missing type id property '@class'; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class br.com.adtsys.ciee.auth.user.data.request.LoginRequest]: missing type id property '@class'\n at [Source: (PushbackInputStream); line: 4, column: 1]",
"path": "/auth/login"

}

我需要进行更多调查,因为我需要禁用该要求