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)
	... 4 more

对应的POJO配置:

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就可以了.

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值