You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Bhaumik Joshi <bh...@outlook.com> on 2016/07/21 13:56:42 UTC

Issue in SolrInputDocument

Hi,

I am getting below error while converting json to my object. I am using Gson class (gson-2.2.4.jar) to generate json from object and object from json.
gson fromJson() method throws below error.
Note: This was working fine with solr-solrj-5.2.0.jar but it causing issue when i uses solr-solrj-6.1.0.jar. As i checked SolrInputDocument class has changed in solr-solrj-5.5.0.

java.lang.IllegalArgumentException: Can not set org.apache.solr.common.SolrInputDocument field com.test.common.MySolrMessage.body to com.google.gson.internal.LinkedTreeMap
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:108)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:185)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:1)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:106)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:185)
at com.google.gson.Gson.fromJson(Gson.java:825)
at com.google.gson.Gson.fromJson(Gson.java:790)
at com.google.gson.Gson.fromJson(Gson.java:739)
at com.google.gson.Gson.fromJson(Gson.java:711)


public class MySolrMessage<T extends SolrInputDocument> implements IMessage
{
    private static final long serialVersionUID = 1L;
    private T body = null;
    private String collection;
    private int action;
    private int errorCode;
    private long msgId;
//few parameterized constructor
//getter and setter method of all above attributes
}

public interface IMessage extends Serializable
{
    public long getMsgId();
    public void setMsgId(long id);
    public Object getBody();
    public void setBody(Object o);
    public void setErrorCode(int ec);
    public int getErrorCode();
}

public class Request {
LinkedList msgList = new LinkedList();

public Request() {
}

public Request(LinkedList l) {
this.msgList = l;
}

public LinkedList getMsgList() {
return this.msgList;
}
}

@JsonAutoDetect(JsonMethod.FIELD)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Request2
{
    @JsonProperty
    @JsonDeserialize(as=LinkedList.class,contentAs = MySolrMessage.class)
    LinkedList<MySolrMessage<SolrInputDocument>> msgList = new LinkedList<MySolrMessage<SolrInputDocument>>();

    public Request()
    {

    }

    public Request(LinkedList<MySolrMessage<SolrInputDocument>> l)
    {
        this.msgList = l;
    }

    public LinkedList<MySolrMessage<SolrInputDocument>> getMsgList()
    {
        return this.msgList;
    }
}


public class Test {

public static void main(String[] args) {
SolrInputDocument solrDocument = new SolrInputDocument();
solrDocument.addField("id", "1234");
solrDocument.addField("name", "test");
MySolrMessage<SolrInputDocument> asm = new MySolrMessage(solrDocument, "collection1", 1);
IMessage message = asm;
List<IMessage> msgList = new ArrayList<IMessage>();
msgList.add(message);
LinkedList ex = new LinkedList();
ex.addAll(msgList);
Request request = new Request(ex);
try
{
String json = "";
Gson gson = (new GsonBuilder()).serializeNulls().create();
gson.setASessionId((String) null);
json = gson.toJson(request);
Gson gson2 = new Gson();
Request2 retObj = gson2.fromJson(json, Request2.class); //this will gives the above error.
}
catch (Exception e)
{
   e.printStackTrace();
}
}
}

Any idea?



Thanks & Regards,

Bhaumik Joshi