Spring Data Solr创建动态域报错:org.springframework.data.solr.UncategorizedSolrException

今天在项目中使用Spring Data Solr导入动态域数据报错, 控制台打印错误信息如下

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

作者:深海收破烂

深海收破烂
原文地址:https://www.cnblogs.com/itboxue/p/11187550.html