由于使用SolrJ自带的addBean和addBeans老是出现了org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.study.model.SolrItem
然后发现是Solr添加数据的时候加多了个_version_的问题
为了解决这个问题,采用的如下的方法
/**
* SolrUtil帮助类 能够将SolrDocument转换为Java对象
* @Title SolrUtil
* @Description :
* @Author: huangxincheng
* @Date:2017年4月24日
*/
public class SolrUtil {
public static final <T> List<T> getBeans(Class<T> clazz, SolrDocumentList solrDocumentList) throws Exception{
List<T> list = new ArrayList<T>();
T t = null;
for (SolrDocument solrDocument : solrDocumentList) {
//反射出实例
t = clazz.newInstance();
//获取所有属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//判断这个属性上是否存在注解SolrField //存在的话 设置值
if (field.isAnnotationPresent(SolrField.class)) {
//获取注解
SolrField solrField = field.getAnnotation(SolrField.class);
if ("".equals(solrField.value())) {
// 如果注解为默认的 采用此属性的name来从solr中获取值
BeanUtils.setProperty(t, field.getName(), solrDocument.get(field.getName()));
} else {
// 如果注解为不是默认的 采用此注解上的值来从solr中获取值
BeanUtils.setProperty(t, field.getName(), solrDocument.get(solrField.value()));
}
}
}
list.add(t);
}
return list;
}
public static final <T> T getBean(Class<T> clazz, SolrDocument solrDocument) throws Exception {
//反射出实例
T t = clazz.newInstance();
//获取所有属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//判断这个属性上是否存在注解SolrField //存在的话 设置值
if (field.isAnnotationPresent(SolrField.class)) {
//获取注解
SolrField solrField = field.getAnnotation(SolrField.class);
if (SolrDefaultValueFunid.DEFAULT_SOLR_FIEL_VALUE.equals(solrField.value())) {
// 如果注解为默认的 采用此属性的name来从solr中获取值
BeanUtils.setProperty(t, field.getName(), solrDocument.get(field.getName()));
} else {
// 如果注解为不是默认的 采用此注解上的值来从solr中获取值
BeanUtils.setProperty(t, field.getName(), solrDocument.get(solrField.value()));
}
}
}
return t;
}
public static final <T> void addBean(T t, HttpSolrClient httpSolrClient, String solrCore) throws Exception{
Field[] fields = t.getClass().getDeclaredFields();
SolrInputDocument doc = new SolrInputDocument();
for (Field field : fields) {
if (field.isAnnotationPresent(SolrField.class)) {
SolrField solrField = field.getAnnotation(SolrField.class);
if (SolrDefaultValueFunid.DEFAULT_SOLR_FIEL_VALUE.equals(solrField.value())) {
doc.addField(field.getName(), field.get(t));
} else {
doc.addField(solrField.value(), field.get(t));
}
}
}
httpSolrClient.add(solrCore, doc);
httpSolrClient.commit(solrCore);
}
public static final <T> void addBeans(List<T> list, HttpSolrClient httpSolrClient, String solrCore) throws Exception{
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
for (T t : list) {
Field[] fields = t.getClass().getDeclaredFields();
SolrInputDocument doc = new SolrInputDocument();
for (Field field : fields) {
if (field.isAnnotationPresent(SolrField.class)) {
SolrField solrField = field.getAnnotation(SolrField.class);
if (SolrDefaultValueFunid.DEFAULT_SOLR_FIEL_VALUE.equals(solrField.value())) {
doc.addField(field.getName(), field.get(t));
} else {
doc.addField(solrField.value(), field.get(t));
}
}
}
docs.add(doc);
}
httpSolrClient.add(solrCore ,docs);
httpSolrClient.commit(solrCore);
}
public static final void deleteById(HttpSolrClient httpSolrClient, String solrCore, String id) throws Exception{
httpSolrClient.deleteById(solrCore, id);
}
public static final void deleteById(HttpSolrClient httpSolrClient, String solrCore, String[] ids) throws Exception{
httpSolrClient.deleteById(solrCore, Arrays.asList(ids));
}
public static final void deleteById(HttpSolrClient httpSolrClient, String solrCore, List<String> ids) throws Exception{
httpSolrClient.deleteById(solrCore, ids);
}
public static final void deleteByQuery(HttpSolrClient httpSolrClient, String solrCore, String query) throws Exception{
httpSolrClient.deleteByQuery(solrCore, query);
}
}
/**
*
* @Title SolrItem
* @Description :
* @Author: huangxincheng
* @Date:2017年4月24日
*/
public class SolrItem {
@SolrField
public Long id;
@SolrField
public String item_title;
@SolrField
public String item_sell_point;
@SolrField
public Long item_price;
@SolrField
public Integer item_cid;
@SolrField
public String item_image;
@SolrField
public String item_cname;
public Integer getItem_cid() {
return item_cid;
}
public void setItem_cid(Integer item_cid) {
this.item_cid = item_cid;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItem_title() {
return item_title;
}
public void setItem_title(String item_title) {
this.item_title = item_title;
}
public String getItem_sell_point() {
return item_sell_point;
}
public void setItem_sell_point(String item_sell_point) {
this.item_sell_point = item_sell_point;
}
public Long getItem_price() {
return item_price;
}
public void setItem_price(Long item_price) {
this.item_price = item_price;
}
public String getItem_image() {
return item_image;
}
public void setItem_image(String item_image) {
this.item_image = item_image;
}
public String getItem_cname() {
return item_cname;
}
public void setItem_cname(String item_cname) {
this.item_cname = item_cname;
}
@Override
public String toString() {
return "SolrItem [id=" + id + ", item_title=" + item_title + ", item_sell_point=" + item_sell_point
+ ", item_price=" + item_price + ", item_cid=" + item_cid + ", item_image=" + item_image
+ ", item_cname=" + item_cname + "]";
}
}
/**
*
* @Title SolrField
* @Description :
* @Author: huangxincheng
* @Date:2017年4月24日
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SolrField {
String value() default SolrDefaultValueFunid.DEFAULT_SOLR_FIEL_VALUE;
}
/**
*
* @Title SolrDefaultValueFunid
* @Description :
* @Author: huangxincheng
* @Date:2017年4月24日
*/
public interface SolrDefaultValueFunid {
public String DEFAULT_SOLR_FIEL_VALUE = "";
}
本文出自 “12265610” 博客,谢绝转载!
原文地址:http://12275610.blog.51cto.com/12265610/1918901