Search code examples
javajsonjersey

Jersey delivers an empty JSON


I'm working on a project for the university which makes me mad. I need to develop a webservice with jersey, but every request sends me just this empty JSON:

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]  

The database query is test and delivers content. I just don't know what it could be.

Java:

@Path("/getFachbereiche")
public class GetFachbereiche {
    @GET
    @Produces("application/json")
    public Fachbereich[] getFachbereiche() {
        List<Fachbereich>fList = new ArrayList<Fachbereich>();
        Connection conn = MySQLConnection.getInstance();
        if (conn != null) {
            try {
                // Anfrage-Statement erzeugen.
                Statement query;
                query = conn.createStatement();

                // Ergebnistabelle erzeugen und abholen.
                String sql = "SELECT * FROM Fachbereich";
                ResultSet result = query.executeQuery(sql);

                //Ergebniss zurückliefern
                while (result.next()) {
                    fList.add(new Fachbereich(result.getInt(1), result.getString(2)));
                }
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        return fList.toArray(new Fachbereich[fList.size()]);
    }
}

Solution

  • Your attributes from Fachbereich are private, by default, private attributes are not serialized. You have two solutions :

    • Put XmlElement annotation on each attribute so it will be serialized

    • Or define a public getter for each attribute.