当前位置: 首页 > 工具软件 > SuperCSV > 使用案例 >

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

苏乐
2023-12-01

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);
  }
}




 类似资料: