获取com.fasterxml.jackson.databind.exc.invaliddefinitionException
我正在尝试传递具有 json 数组的请求正文,如下所示:
{
"researchItems": [
{
"entityId": "AAAAA",
"entityLevel": "SSS",
"attributeCode": "SSSS",
"effectiveDate": "2022-04-06",
"requestedVendor": "sdsd",
"reasons": "dsdsdsd",
"companyId": "dsd",
"investmentId": "dsd",
"shareClassId": "ddfgfg",
"riskRating": 2,
"source": "weweew"
}
]
}
我使用 lombok 创建了一个类 ResearchItems 作为 POJO,如下所示
package apiActions;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
@AllArgsConstructor
@NoArgsConstructor
@Setter
public class researchItems {
String entityId;
String entityLevel;
String attributeCode;
String effectiveDate;
String requestedVendor;
String reasons;
String companyId;
String investmentId;
String shareClassId;
String riskRating;
String source;
}
现在我正在尝试编写以下代码以使用放心将其作为请求正文传递并获得响应。
researchItems rst=new researchItems("AAAAA", "SSS", "SSSS", "2022-04-06", "sdsd", "dsdsdsd", "0C00000OFM", "dsd", "ddfgfg", "2", "weweew");
//ObjectMapper objmap=new ObjectMapper();
List<researchItems> l1=new ArrayList<>();
l1.add(rst);
httpRequest.header("Content-Type","application/json").header("Authorization", "Bearer"+Create_Research_ticket.getbearertoken());
Response response = httpRequest.body(reqbody).post("/research-data/research-item/create");
String responseBody=response.getBody().asString();
System.out.println("Response Body is:" +responseBody);
//status code validation
int statusCode=response.getStatusCode();
System.out.println("Status code is: "+statusCode);
Assert.assertEquals(statusCode, 200);
当我执行时,出现以下异常
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class apiActions.researchItems and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚首先检查了Javadocs,看来您缺少代码中的Getters,这就是为什么序列化失败的原因。
参考:
我在这里也发现了同样的解释:
https://wwwww.baeldung.com/jackson-jackson-jsonmappingexception
I just checked javadocs first and it seems you are missing getters in your code, thats why serialization fails.
Ref:
https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/SerializationFeature.html#FAIL_ON_EMPTY_BEANS
Also I found same explaination here:
https://www.baeldung.com/jackson-jsonmappingexception