获取 com.fasterxml.jackson.databind.exc.InvalidDefinitionException

Getting 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 创建了一个 class 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)

我刚刚先检查了 javadoc,看来您的代码中缺少 getter,这就是序列化失败的原因。

参考: https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/SerializationFeature.html#FAIL_ON_EMPTY_BEANS

我也在这里找到了相同的解释: https://www.baeldung.com/jackson-jsonmappingexception