Home > Back-end >  Getting com.fasterxml.jackson.databind.exc.InvalidDefinitionException
Getting com.fasterxml.jackson.databind.exc.InvalidDefinitionException

Time:04-12

I am trying to pass request body which has a json array as follows:

{
    "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"
        }
    ]
}

I created a class researchItems as POJO as follows using lombok

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;

}

Now I am trying to write the following code to pass this as a request body using rest assured and get a response.

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);

When I execute, I am getting the following exception

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)

CodePudding user response:

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

  • Related