1
I am developing a web service using Jersey and the Json returned by one of the methods is coming empty. The strange thing is that the other methods are following the same logic and are working correctly. I debugged and checked that the data is returned from the database and my list with the objects is created correctly and that the Answer contains the list. Follows the code:
Method 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 the part of the controller
public ArrayList<Aluno> retornaAluno(String nomeEscola, String codigoResponsavel) {
ArrayList<Aluno> listaAluno = new WSDao(nomeEscola).retornaAluno(codigoResponsavel);
return listaAluno;
}
Method remaining in the class communicating 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");
}
}
Pupil 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 file.xml
<?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>
How json is being returned when I try to return only one object, for example
[
{}
]
I managed to solve the problem. The problem was in the Student class. I had removed the get methods from each attribute as they were not being used anywhere in my code, but when I put back the get methods from the attributes in the Student class the data returned to Json. For some reason when serializing Jersey uses the get methods of attributes, then the tip is: if you use Jersey to build a web service in the Bean classes let the get methods even if they are not used by other classes.
– Luiz Macedo
I posted as an answer to the problem resolution, so you help other people who come to have the same problem.
– stderr