I'm developing a web service using Jersey and Json returned by one of the methods is coming empty. The strangest thing is that the other methods are following the same logic and are working properly. I made a debug and checked that the data is returned from the database and my list with the objects is created correctly and the response contains the list. Here is the code:
Method that is in the resource class
@GET
@Path("/retornaAluno/{nomeEscola}/{codigoResponsavel}")
@Produces(MediaType.APPLICATION_JSON)
public Response retornaAluno(@PathParam("nomeEscola") String nomeEscola, @PathParam("codigoResponsavel") String codigoResponsavel) {
Response response;
ArrayList<Aluno> listaAluno = new Controler().retornaAluno(nomeEscola, codigoResponsavel);
if(listaAluno != null){
logger.info("Resposta do retornaAluno enviada com sucesso");
//se no entity eu colocar algum texto, por exemplo, no json é exibido corretamente
response = Response.status(200).entity(listaAluno).build();
return response;
}else{
logger.info(MSG_COD_RESP_NAO_ENCONTRADO);
response = Response.status(200).entity(MSG_COD_RESP_NAO_ENCONTRADO).build();
return response;
}
}
Method that stays in the class that makes up the controler
public ArrayList<Aluno> retornaAluno(String nomeEscola, String codigoResponsavel) {
ArrayList<Aluno> listaAluno = new WSDao(nomeEscola).retornaAluno(codigoResponsavel);
return listaAluno;
}
Method that is in the class that communicates with the bank
public ArrayList<Aluno> retornaAluno(String codigoResponsavel) {
try {
if(!verificaCodigoNoMetodo(codigoResponsavel).equals("0")){
ArrayList<Aluno> listaAluno = new ArrayList<Aluno>();
String sql = "SELECT A.ID, A.NOME, A.SERIE, A.TURMA FROM TB_ALUNO A INNER JOIN TB_ALUNO_RESPONSAVEL B ON B.ALUNO_ID = A.ID "
+ "INNER JOIN TB_CODIGO C ON B.RESPONSAVEL_ID = C.RESPONSAVELID WHERE C.IDALUNO IS NULL AND C.CODIGO = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, codigoResponsavel);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Aluno aluno = new Aluno();
aluno.setIdAluno(rs.getInt("id"));
aluno.setNome(rs.getString("nome"));
aluno.setSerie(rs.getString("serie"));
aluno.setTurma(rs.getString("turma"));
listaAluno.add(aluno);
}
stmt.close();
rs.close();
connection.close();
logger.info("retornaAluno chamado com sucesso.");
return listaAluno;
}else{
logger.info("retornaAluno chamado com sucesso.");
return null;
}
} catch (SQLException e) {
logger.error("Ocorreu uma falha na consulta no método retornaAluno", e);
throw new RuntimeException("Ocorreu uma falha na consulta no método retornaAluno");
}
}
Student Class
package Bean;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Aluno {
private String nome;
private String codigo;
private String serie;
private String turma;
private int idAluno;
public void setSerie(String serie) {
this.serie = serie;
}
public void setTurma(String turma) {
this.turma = turma;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setCodigo(String codigo) {
this.codigo = codigo;
}
public void setIdAluno(int idAluno) {
this.idAluno = idAluno;
}
}
Web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Nome_do_projeto</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>pacote_onde_fica_os_recursos</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>log4jContextName</param-name>
<param-value>Nome_do_projeto</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
As json is being returned when I try to return only one object, for example
[
{}
]