今天在项目中使用Spring Data Solr导入动态域数据报错, 控制台打印错误信息如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Exception in thread "main" org.springframework.data.solr.UncategorizedSolrException: nested exception is java.lang.NullPointerException at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java: 145 ) at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java: 199 ) at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java: 194 ) at com.pinyougou.solrutil.ImportItem.importAllItem(ImportItem.java: 46 ) at com.pinyougou.solrutil.ImportItem.main(ImportItem.java: 53 ) Caused by: java.lang.NullPointerException at org.springframework.data.solr.core.convert.MappingSolrConverter.writeWildcardMapPropertyToTarget(MappingSolrConverter.java: 310 ) at org.springframework.data.solr.core.convert.MappingSolrConverter.access$ 100 (MappingSolrConverter.java: 62 ) at org.springframework.data.solr.core.convert.MappingSolrConverter$ 2 .doWithPersistentProperty(MappingSolrConverter.java: 287 ) at org.springframework.data.solr.core.convert.MappingSolrConverter$ 2 .doWithPersistentProperty(MappingSolrConverter.java: 269 ) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java: 311 ) at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java: 269 ) at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java: 258 ) at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java: 62 ) at org.springframework.data.solr.core.SolrTemplate.convertBeanToSolrInputDocument(SolrTemplate.java: 463 ) at org.springframework.data.solr.core.SolrTemplate.convertBeansToSolrInputDocuments(SolrTemplate.java: 546 ) at org.springframework.data.solr.core.SolrTemplate.access$ 100 (SolrTemplate.java: 91 ) at org.springframework.data.solr.core.SolrTemplate$ 5 .doInSolr(SolrTemplate.java: 202 ) at org.springframework.data.solr.core.SolrTemplate$ 5 .doInSolr(SolrTemplate.java: 199 ) at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java: 141 ) |
public class TbItem implements Serializable { @Field private Long id; //其他属性略 @Dynamic //动态域 @Field("item_spec_*") private Map specMap; //set() get()略 !
@Component("importItem") public class ImportItem { @Autowired private SolrTemplate solrTemplate; @Autowired private TbItemMapper itemMapper; private void importAllItem(){ TbItemExample example = new TbItemExample(); TbItemExample.Criteria criteria = example.createCriteria(); criteria.andStatusEqualTo("1");//只查询状态为 1 的数据 List<TbItem> list = itemMapper.selectByExample(example); for (TbItem item : list) { /* spec是POJO中另外的属性 item.getSpec() 得到数据结构: {"网络":"移动4G","机身内存":"32G"} 使用fastjson 转换为Map,存放在POJO中 */ Map specMap = JSON.parseObject(item.getSpec(), Map.class); item.setSpecMap(specMap); } /* *统一保存 */ solrTemplate.saveBeans(list); solrTemplate.commit(); } public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml"); ImportItem util = (ImportItem) applicationContext.getBean("importItem"); util.importAllItem(); } }
在网上没有找到对应的解决方案,不断尝试,将POJO中Map的范型加上后结果OK!!!
@Dynamic //动态域 @Field("item_spec_*") private Map<String,String> specMap;//最好将范型加上 //set() get()略
因为是Maven分模块开发,将POJO重新install就可以了
2019-07-1510:47:32
作者:深海收破烂
深海收破烂
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步