csv 文件上传异常org.supercsv.exception.SuperCsvReflectionException

web端浏览csv格式文件 文件是手机号13811111111

发现报错:找不到方法set13811111111

org.supercsv.exception.SuperCsvReflectionException: unable to find method set13811111111(java.lang.String) in class net.guodulink.gdfms.sms.entity.CsvSmsPModel - check that the corresponding nameMapping element matches the field name in the bean, and the cell processor returns a type compatible with the field

        at org.supercsv.util.ReflectionUtils.findSetter(ReflectionUtils.java:185) ~[super-csv-2.4.0.jar:?]
        at org.supercsv.util.MethodCache.getSetMethod(MethodCache.java:96) ~[super-csv-2.4.0.jar:?]
        at org.supercsv.io.CsvBeanReader.populateBean(CsvBeanReader.java:154) ~[super-csv-2.4.0.jar:?]
        at org.supercsv.io.CsvBeanReader.readIntoBean(CsvBeanReader.java:264) ~[super-csv-2.4.0.jar:?]
        at org.supercsv.io.CsvBeanReader.read(CsvBeanReader.java:190) ~[super-csv-2.4.0.jar:?]
        at net.guodulink.gdfms.sms.utils.CsvUtils.readWithCsvPReader(CsvUtils.java:110) [CsvUtils.class:?]
        at net.guodulink.gdfms.sms.utils.CsvSmsHandlerThread.sameCsvContentHandler(CsvSmsHandlerThread.java:80) [CsvSmsHandlerThread.class:?]
        at net.guodulink.gdfms.sms.utils.CsvSmsHandlerThread.csvUpload(CsvSmsHandlerThread.java:64) [CsvSmsHandlerThread.class:?]
        at net.guodulink.gdfms.sms.utils.CsvSmsHandlerThread.run(CsvSmsHandlerThread.java:54) [CsvSmsHandlerThread.class:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_91]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_91]

        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]

原因:

csv格式需要标头,文件加上表头phone 和 CsvSmsPMode自定义beanl中的方法名一样即可。

super-csv-2.4.0.jar中的部分源码:

CsvBeanReader
package org.supercsv.io;


public <T> T read(Class<T> clazz, String[] nameMapping)
    throws IOException
  {
    if (clazz == null)
      throw new NullPointerException("clazz should not be null");
    if (nameMapping == null) {
      throw new NullPointerException("nameMapping should not be null");
    }

    return readIntoBean(instantiateBean(clazz), nameMapping, null);
  }  
private <T> T readIntoBean(T bean, String[] nameMapping, CellProcessor[] processors)
    throws IOException
  {
    if (readRow()) {
      if (nameMapping.length != length()) {
        throw new IllegalArgumentException(String.format("the nameMapping array and the number of columns read should be the same size (nameMapping length = %d, columns = %d)", new Object[] { 
          Integer.valueOf(nameMapping.length), 
          Integer.valueOf(length()) }));
      }


      if (processors == null) {
        this.processedColumns.clear();
        this.processedColumns.addAll(getColumns());
      } else {
        executeProcessors(this.processedColumns, processors);
      }


      return populateBean(bean, nameMapping);
    }


    return null;
  }
private <T> T populateBean(T resultBean, String[] nameMapping)
  {
    for (int i = 0; i < nameMapping.length; i++)
    {
      Object fieldValue = this.processedColumns.get(i);


      if ((nameMapping[i] != null) && (fieldValue != null))
      {
        Method setMethod = this.cache.getSetMethod(resultBean, nameMapping[i], fieldValue.getClass());
        invokeSetter(resultBean, setMethod, fieldValue);
      }
    }


    return resultBean;
  }

MethodCache
  public <T> Method getSetMethod(Object object, String fieldName, Class<?> argumentType)
  {
    if (object == null)
      throw new NullPointerException("object should not be null");
    if (fieldName == null)
      throw new NullPointerException("fieldName should not be null");
    if (argumentType == null) {
      throw new NullPointerException("argumentType should not be null");
    }

    Method method = (Method)this.setMethodsCache.get(object.getClass(), argumentType, fieldName);
    if (method == null) {
      method = ReflectionUtils.findSetter(object, fieldName, argumentType);
      this.setMethodsCache.set(object.getClass(), argumentType, fieldName, method);
    }
    return method;
  }

package org.supercsv.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.supercsv.exception.SuperCsvReflectionException;

public final class ReflectionUtils
{
  public static final String SET_PREFIX = "set";
  public static final String GET_PREFIX = "get";
  public static final String IS_PREFIX = "is";
  private static final Map<Class<?>, Class<?>> AUTOBOXING_CONVERTER = new HashMap();

  public static Method findGetter(Object object, String fieldName)
  {
    if (object == null)
      throw new NullPointerException("object should not be null");
    if (fieldName == null) {
      throw new NullPointerException("fieldName should not be null");
    }

    Class clazz = object.getClass();

    String standardGetterName = getMethodNameForField("get", fieldName);
    Method getter = findGetterWithCompatibleReturnType(standardGetterName, clazz, false);

    if (getter == null) {
      String booleanGetterName = getMethodNameForField("is", fieldName);
      getter = findGetterWithCompatibleReturnType(booleanGetterName, clazz, true);
    }

    if (getter == null)
    {
      throw new SuperCsvReflectionException(
        String.format("unable to find getter for field %s in class %s - check that the corresponding nameMapping element matches the field name in the bean", new Object[] { fieldName, clazz
        .getName() }));
    }

    return getter;
  }

  private static Method findGetterWithCompatibleReturnType(String getterName, Class<?> clazz, boolean enforceBooleanReturnType)
  {
    for (Method method : clazz.getMethods())
    {
      if ((getterName.equalsIgnoreCase(method.getName())) && (method.getParameterTypes().length == 0) && 
        (!method
        .getReturnType().equals(Void.TYPE)))
      {
        if ((!enforceBooleanReturnType) || (Boolean.TYPE.equals(method.getReturnType())) || 
          (Boolean.class
          .equals(method
          .getReturnType()))) {
          return method;
        }
      }
    }

    return null;
  }

  public static Method findSetter(Object object, String fieldName, Class<?> argumentType)
  {
    if (object == null)
      throw new NullPointerException("object should not be null");
    if (fieldName == null)
      throw new NullPointerException("fieldName should not be null");
    if (argumentType == null) {
      throw new NullPointerException("argumentType should not be null");
    }

    String setterName = getMethodNameForField("set", fieldName);
    Class clazz = object.getClass();

    Method setter = findSetterWithCompatibleParamType(clazz, setterName, argumentType);

    if ((setter == null) && (AUTOBOXING_CONVERTER.containsKey(argumentType))) {
      setter = findSetterWithCompatibleParamType(clazz, setterName, (Class)AUTOBOXING_CONVERTER.get(argumentType));
    }

    if (setter == null)
    {
      throw new SuperCsvReflectionException(
        String.format("unable to find method %s(%s) in class %s - check that the corresponding nameMapping element matches the field name in the bean, and the cell processor returns a type compatible with the field", new Object[] { setterName, argumentType
        .getName(), clazz.getName() }));
    }

    return setter;
  }

  private static Method findSetterWithCompatibleParamType(Class<?> clazz, String setterName, Class<?> argumentType)
  {
    Method compatibleSetter = null;
    for (Method method : clazz.getMethods())
    {
      if ((setterName.equalsIgnoreCase(method.getName())) && (method.getParameterTypes().length == 1))
      {
        Class parameterType = method.getParameterTypes()[0];
        if (parameterType.equals(argumentType)) {
          compatibleSetter = method;
          break;
        }
        if (parameterType.isAssignableFrom(argumentType)) {
          compatibleSetter = method;
        }
      }
    }

    return compatibleSetter;
  }

  private static String getMethodNameForField(String prefix, String fieldName)
  {
    return prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);//setPhone

  }

  static
  {
    AUTOBOXING_CONVERTER.put(Long.TYPE, Long.class);
    AUTOBOXING_CONVERTER.put(Long.class, Long.TYPE);
    AUTOBOXING_CONVERTER.put(Integer.TYPE, Integer.class);
    AUTOBOXING_CONVERTER.put(Integer.class, Integer.TYPE);
    AUTOBOXING_CONVERTER.put(Character.TYPE, Character.class);
    AUTOBOXING_CONVERTER.put(Character.class, Character.TYPE);
    AUTOBOXING_CONVERTER.put(Byte.TYPE, Byte.class);
    AUTOBOXING_CONVERTER.put(Byte.class, Byte.TYPE);
    AUTOBOXING_CONVERTER.put(Short.TYPE, Short.class);
    AUTOBOXING_CONVERTER.put(Short.class, Short.TYPE);
    AUTOBOXING_CONVERTER.put(Boolean.TYPE, Boolean.class);
    AUTOBOXING_CONVERTER.put(Boolean.class, Boolean.TYPE);
    AUTOBOXING_CONVERTER.put(Double.TYPE, Double.class);
    AUTOBOXING_CONVERTER.put(Double.class, Double.TYPE);
    AUTOBOXING_CONVERTER.put(Float.TYPE, Float.class);
    AUTOBOXING_CONVERTER.put(Float.class, Float.TYPE);
  }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值