Estou tendo problemas ao trabalhar JSON e Integer.
Método JSON:
/**
* Transform json data format into Contact object
*
* @param data - json data from request
* @return
*/
private Contact getContactFromJSON(Object data) {
JSONObject jsonObject = JSONObject.fromObject(data);
Contact newContact = (Contact) JSONObject.toBean(jsonObject, Contact.class);
return newContact;
}
Service:
/**
* Delete contact/contacts
*
* @param data - json data from request
*/
@Transactional
public void delete(Object data) {
//it is an array - have to cast to array object
if (data.toString().indexOf('[') >= 0) {
List<Integer> deleteContacts = util.getListIdFromJSON(data);
for (Integer id : deleteContacts) {
contactDAO.deleteContact(id);
}
} else { //it is only one object - cast to object/bean
Integer id = Integer.parseInt(data.toString());
contactDAO.deleteContact(id);
}
}
Controller:
@RequestMapping(value = "/contact/delete.action")
public @ResponseBody
Map<String, ? extends Object> delete(@RequestParam Object data) throws Exception {
Map response = new HashMap();
try {
contactService.delete(data);
response.put("success", true);
//Map<String, Object> modelMap = new HashMap<>(3);
//modelMap.put("success", true);
return response;
} catch (Exception e) {
return getModelMapError("Erro ao tentar excluir contatos." + e);
//System.out.println("erro: " + e);
}
}
Depurei a aplicação o HashMap está com valor 0.
E o Exception me reporta o erro:
net.sf.json.JSONException: java.lang.NoSuchMethodException : java.lang.Integer.()
Quem poder ajudar agradeço grato desde já