Matheus Bica Matheus Bica - 4 months ago 7
JSON Question

How to Receive List JSON at POST Filtering Errors

I'am receiving an array list of objects called Incidents.

But when I receive any object on that list that doesn't meet the requirements to be inserted at my DB, it simply ignore the entire list and returns error 500.

I would like to know how to manage this, in a way that only the Incidents that have error are not inserted.

And if possible how can I know which ones have error and which error it is.

Here is my actual code, I'm using Spring REST.

At Incident DAO:

@Transactional
public List<Incident> createIncident(List<Incident> incident) {
for (int i = 0; i < incident.size(); i++) {
try {
em.persist(incident.get(i));
} finally {

}
}
return incident;
}


At IncidentController:

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody List<Incident> create(@RequestBody List<Incident> incident) {
return incidentDao.createIncident(incident);
}


This is my latest try. I did an try to just ignore the ones that ends in an error but that just did the same I said before.

Thanks for any help, I'am new to Java and REST.

EDIT: Latest try update
Controller:

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody List<Incident> create(@RequestBody List<Incident> incident) {
if (incident != null) {
for (int i = 0; i < incident.size(); i++) {
incidentDao.createIncident(incident.get(i));
}
}
return incident;
}


DAO:

@Transactional
public boolean createIncident(Incident incident) {
try{
em.persist(incident);
return true;
}
catch(Exception e){
return false;
}
}


What I understood so far is that, my webservice function is treating this as an unique transaction so when one Incident from the list cannot commit, it's entire transaction rollback.

EDIT(2): Solution found, but I guess it's not the ideal one, if someone finds another way more direct, I'll be happy!

I had to create an Method on my composite primary key class, to set and get the arguments and created a search.

So, if this search returns something besides null it doesn't even try to add, if this returns null, that means that my 2 PK's are not found in my table, so the incident can be added(it added another search before each insert which is dumb, but I don't know another way right now).

public Incident getIncidentByIDandDate(String incidentID, String date) {
//here comes the PK's class with the info I'm searching setted
IncidentPK PK = new IncidentPK();
PK.setIncidentID(incidentID);
PK.setdate(date);

Incident incident = em.find(Incident.class, PK);
return incident;
} //if returns null means that no incident with this info's were found


Thanks! Hope this helps someone else if they doesn't find another solution.

Answer

Solution found, but I guess it's not the ideal one, if someone finds another way more direct, I'll be happy!

I had to create an Method on my composite primary key class, to set and get the arguments and created a search.

So, if this search returns something besides null it doesn't even try to add, if this returns null, that means that my 2 PK's are not found in my table, so the incident can be added(it added another search before each insert which is dumb, but I don't know another way right now).

  public Incident getIncidentByIDandDate(String incidentID, String date) {
//here comes the PK's class with the info I'm searching setted
                IncidentPK PK = new IncidentPK();
                PK.setIncidentID(incidentID);
                PK.setdate(date);

                Incident incident = em.find(Incident.class, PK);
                return incident;    
        } //if returns null means that no incident with this info's were found

Thanks! Hope this helps someone else if they doesn't find another solution.

Comments