0

I have written a java class to retrieve schema from schema registry using feignHttp. Above exception is thrown when I try to retrieve a schema.

Client class:

import com.google.gson.internal.LinkedTreeMap;
import feign.Feign;
import feign.FeignException;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.okhttp.OkHttpClient;
import org.apache.avro.Schema;
import org.apache.avro.SchemaParseException;

public class SchemaRegistryReader {
    public Schema getSchemaFromID(String registryURL, String schemaID) throws SchemaParseException, FeignException {
        SchemaRegistryClient registryClient = Feign.builder()
                .client(new OkHttpClient())
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .target(SchemaRegistryClient.class, registryURL);
        Object returnedSchema = registryClient.findByID(schemaID);
        LinkedTreeMap jsonSchemaObject = ((LinkedTreeMap) returnedSchema);
        String jsonSchema = jsonSchemaObject.get("schema").toString();
        return new Schema.Parser().parse(jsonSchema);
    }
}

Method Interface:

import feign.Headers;
import feign.Param;
import feign.RequestLine;

public interface SchemaRegistryClient {
    @RequestLine("GET")
    Object connect();

    @RequestLine("GET /schemas/ids/{id}")
    @Headers("Content-Type: application/json")
    Object findByID(@Param("id") String id);
}

Exception thrown:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.google.gson.internal.LinkedTreeMap at org.wso2.extension.siddhi.map.avro.util.schema.SchemaRegistryReader.getSchemaFromID(SchemaRegistryReader.java:41)

Would be great help if the cause for this can be found.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Shenzo
  • 31
  • 4
  • Can you check whether https://stackoverflow.com/questions/826319/classcastexception-when-casting-to-the-same-class is applicable to your situation? – fvu Dec 19 '18 at 12:24
  • Just out of interest: If you cannot cast to `LinkedTreeMap`, what happens if you just don't do it, like `LinkedTreeMap jsonSchemaObject = returnedSchema;`? Have you tried it? Does it throw another `Exception`? – deHaar Dec 19 '18 at 12:28
  • Is it possible that "findById" return map instead of 1 object ? – Rafał Sokalski Dec 19 '18 at 12:30
  • Why Feign? `kafka-schema-registry-client` Maven artifact already includes an HTTP client and a `Schema` object that it returns – OneCricketeer Dec 20 '18 at 19:56

0 Answers0