2019-03-09 09:39:12.006 ERROR 1564 --- [nio-9104-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/search] threw exception [Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException] with root cause
java.lang.RuntimeException: org.springframework.data.solr.UncategorizedSolrException: Can't resolve required map value type for interface java.util.Map!; nested exception is java.lang.IllegalStateException: Can't resolve required map value type for interface java.util.Map!
org.springframework.data.solr.UncategorizedSolrException: Can't resolve required map value type for interface java.util.Map!; nested exception is java.lang.IllegalStateException: Can't resolve required map value type for interface java.util.Map!
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:171)
at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:227)
at org.springframework.data.solr.core.SolrOperations.saveBeans(SolrOperations.java:160)
...
at java.lang.Thread.run(Thread.java:748)
应该改成
同理,在json解析的时候也要注意接口与实现类的使用。
总结:
- 在json解析的时候,java会利用反射创建对象,解析json赋值,因此不要使用Map.class,要使用HashMap.class。
HashMap specMap = JSON.parseObject(item.getSpec(),HashMap.class);
- 在实体类中的对象,若要作为返回值,应注意接口与实现类的使用,比如solr,不能解析Map接口。因为要创建对象。
Can't resolve required map value type for interface java.util.Map!
- 在mybatis中的返回值,也不要写List或Map的接口,要使用ArrayList,HashMap实现类。
- 接口不能实例化!!!!