Skip to content

Null pointer exception when calling jp.getCodec() in custom deserializer/ JsonParser has null codec object  #120

Closed
@hwangdylan

Description

@hwangdylan

I have an elasticsearch client that makes a query to the ES instance and uses a custom deserializer to convert the json object to a very simple POJO object. However I keep getting the error that jp.getCodec() is null.

Here is the config for my es client

    @Bean
    public ElasticsearchClient setUp() {

        // Create the low-level client
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, mapper());

        // And create the API client
        return new ElasticsearchClient(transport);
    }

Here's the query that I am making to ES.

    public void find2() {
        try {
            SearchResponse<test> search = client.search(s -> s
                            .index("test")
                            .query(q -> q
                                    .term(t -> t
                                            .field("age")
                                            .value(v -> v.stringValue("2"))
                                    )),
                    test.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.toString());
        }
    }

Here is my test object that I am deserializing it to:

@JsonDeserialize(using = test.testDeserializer.class)
public class test {
    String model;
    String age;

    public static class testDeserializer extends JsonDeserializer<test> {

        @Override
        public test deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            JsonNode productNode = jp.getCodec().readTree(jp);

            test res = new test();
            if (productNode.has("age")) {
                res.age = productNode.get("age").asText();
            }
            if (productNode.has("model")) {
                res.model = productNode.get("model").asText();
            }
            return res;
        }

    }
}

Any ideaswhy jp.getCodec() keeps returning null? I know the deserializer works as I tested it when calling it directly, I just don't know where the call is being made to the deserializer when running the ES query an why no codec is being passed in.

When using the @JsonProperty("model") annotation, it was able to serialize and deserialize perfectly fine, just not sure why the custom deserializer does not work.

Is this a bug or does codec need to be setup manually when configuring the client?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions