0

I'm using spring with JdbcTemplates, java 1.8

I have the next class instead of rowMapper, and it works well:

public class NestedRowMapper<T> implements RowMapper<T> {
private final Class<T> mappedClass;

public NestedRowMapper(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
}

@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {

    T mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);

    bw.setAutoGrowNestedPaths(true);

    ResultSetMetaData meta_data = rs.getMetaData();
    int columnCount = meta_data.getColumnCount();

    for (int index = 1; index <= columnCount; index++) {

        try {

            String column = JdbcUtils.lookupColumnName(meta_data, index);
            JdbcUtils.lookupColumnName(meta_data, index);
            Object value = JdbcUtils.getResultSetValue(rs, index, Class.forName(meta_data.getColumnClassName(index)));
            bw.setPropertyValue(column, value);

        } catch (TypeMismatchException | NotWritablePropertyException | ClassNotFoundException e) {
            // Ignore
        }
    }

    return mappedObject;
}

It works perfectly and I get the next structure:

"usuario": {
"PUD_USUARIO": "pruebas@rgp.org.gt",
"portalPersona": {
    "PP_NOMBRES": "EDWIN",
    "PP_APELLIDOS": "TELLO",
    "PP_TELEFONO": "35202684",    
  },
"perfil": {
    "PER_NOMBRE": "ROLE_USUARIO_GENERAL",
    "PER_DESCRIPCION": "USUARIO GENERAL PORTAL WEB",
    "PER_ESTADO": "1"
},
"grupo": {
    "miembros": {
        "PGU_ID": 7,
        "PGU_USUARIO": "pruebas@rgp.org.gt"
        "PGU_ESTADO": "1"
    }
  }
}

But I would like to have an array as result in miembros like this:

"usuario": {
    "PUD_USUARIO": "pruebas@rgp.org.gt",
    "portalPersona": {
        "PP_NOMBRES": "EDWIN",
        "PP_APELLIDOS": "TELLO",
        "PP_TELEFONO": "35202684",    
      },
    "perfil": {
        "PER_NOMBRE": "ROLE_USUARIO_GENERAL",
        "PER_DESCRIPCION": "USUARIO GENERAL PORTAL WEB",
        "PER_ESTADO": "1"
    },
    "grupo": {
        "miembros": [{
            "PGU_ID": 7,
            "PGU_USUARIO": "pruebas@rgp.org.gt"
            "PGU_ESTADO": "1"
        }]
      }
    }

Just declaring miembros as a list doesn't work.

How could a do it?

ano
  • 27
  • 2
Alex
  • 671
  • 5
  • 19

0 Answers0