Return Json of type "field_name": value with Java and Spring

1

I'm doing a REST API that consumes data from a mySQL database and returns it in JSON format.

I use hibernate, so I have object entities "connected" to the database with their respective notations @Entity, Id etc. and these are returned perfectly.

But apart, I have objects that are formed from queries but I do not want to store them in the database, so they do not have any notations or a table in the database.

Problem? These objects are returned in JSON in the following way (Those that do not have notations):

[
    [
        4,
        "media",
        "status",
        1497736800000,
        1503180000000
    ],
    [
        5,
        "alta",
        "status",
        1497736800000,
        1503180000000
    ]
]

When the others are returned like this, in "key" format: value:

[
    {
        "id_team": 1,
        "date": 1497887607000,
        "level": 0,
        "id_project": 1
    },
    {
        "id_team": 1,
        "date": 1497987784000,
        "level": 0,
        "id_project": 1
    },
]

The method that returns the "incorrect" form is this:

public List<TechnicalDependency> getUserTecDep(int id_project, int id_factory, int id_team, String id_user) 
{
    return (List<TechnicalDependency>)manager.createQuery("SELECT id_task, priority, status, init_date, duedate "
            + "FROM Task "
            + "WHERE assignee = '"+ id_user +"'").getResultList();

}

The method that returns values in JSON as I wish is this:

public List<Team_Level> getTeamLevels(int id_team, int id_project, String date1, String date2) 
{
    return (List<Team_Level>) manager.createQuery(
                "FROM Team_Level"
                + "WHERE date >= '"+date1+"' "
                + "AND date <= '"+date2+"' "
                + "AND id_team ='"+id_team+"' "
                + "AND id_project = "+ id_project).getResultList();
}

The methods are the same, so the error I understand is in the way of mapping to JSON. The problem is that later I can not (or do not know) map it back to object if I do not have the key-value.

    
asked by SBRM 25.06.2017 в 03:23
source

1 answer

0

The problem is not that the objects come or do not come from the database, the problem is that what you recover from the database is not what you think.

return (List<TechnicalDependency>)manager.createQuery("SELECT id_task, priority, status, init_date, duedate "
        + "FROM Task "
        + "WHERE assignee = '"+ id_user +"'").getResultList();

This returns a List<Object[]> . The type returned by createQuery varies according to the query, and the casting only checks that it is a List , regardless of the parameterization. The generics information is not in the compiled class so this check can not be done at run time, this is called type erasure . You can see more details at this answer from me on the site at English .

So when you pass to the JSON library a List<Object[]> , in JSON it remains as a list of lists.

If you want to obtain an object from each row of the query, the correct way is

return (List<TechnicalDependency>)manager.createQuery(
        "SELECT new TechnicalDependency(id_task, priority, status, init_date, duedate) "
        + "FROM Task "
        + "WHERE assignee = '"+ id_user +"'").getResultList();

Naturally, your class should have the right constructor.

In general, whenever there are problems that mix several layers (databases, JSON, etc.), it is a good idea to try to verify in the intermediate steps that what you have is really what you expect. Debugging a program from the web layer to the database "all together" is sooo difficult.

    
answered by 25.06.2017 / 04:38
source