I have a Map as followed:
final Map<String, String[]> paramMap = multipartRequest.getParameterMap();
and im filling the values using reflections into a model:
final Class<?> requestClass = config.getModelClazz();
Object requestModel = new Object();
try {
requestModel = requestClass.newInstance();
} catch (InstantiationException | IllegalAccessException e1) {
LOGGER.warn("Failed while creating instance: " + e1);
}
for (final Field field : requestClass.getDeclaredFields()) {
field.setAccessible(true);
for (final Map.Entry<String, String[]> entry : paramMap.entrySet()) {
if (field.getName().equals(entry.getKey())) {
try {
field.set(requestModel, entry.getValue()[0]);
} catch (IllegalArgumentException | IllegalAccessException e1) {
LOGGER.warn("Failed while setting value: " + e1);
}
}
}
}
The problem is that field.set()
can only set values from the model which are strings, since the value-entry of the map is a string too. I know that spring somehow can convert request-params in primitive types. For example:
if you call the url /test.view?age=18&isMale=true
and have a GET-Controller as followed:
@RequestMapping(value = "/test.view", method = RequestMethod.GET)
public String openView(@ModelAttribute(value = "test") final Test test){
return "test";
}
public class Test {
private Integer age;
private Boolean isMale;
//getter & setter
}
the request-params are getting filled into the model, which are not only Strings
Does anyone know, how exacly they are mapping the request-params to other types? Or do you have any different approach for my problem?
Aucun commentaire:
Enregistrer un commentaire