Ignore field when converting to JSON in a REST service in JAVA EE

1

I have the following code:

@Path("alumnos")
public class AlumnosController {

@GET
@Produces("application/json")
public List<Alumno> getAlumnos() {
    List<Alumno> alumnos = new ArrayList<Alumno>();
    alumnos.add(new Alumno("Javier Octavio", 32));
    alumnos.add(new Alumno("Gerardo Anibal", 32));
    return alumnos;
}

class Alumno {
    private String nombre;
    @JsonIgnore
    private int edad;

    /**
     * @param nombre
     * @param edad
     */
    public Alumno(String nombre, int edad) {
        super();
        this.nombre = nombre;
        this.edad = edad;
    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre
     *            the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the edad
     */
    @JsonIgnore
    public int getEdad() {
        return edad;
    }

    /**
     * @param edad
     *            the edad to set
     */
    public void setEdad(int edad) {
        this.edad = edad;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Alumno [nombre=" + nombre + ", edad=" + edad + "]";
    }

}

}

I wish to ignore the age field, I used the JsonIgnore annotation but it does not work when I access the url / students it returns me the json with the age.

    
asked by Horacio Sanchez 14.11.2016 в 20:27
source

1 answer

0

Try scoring your class like this:

Example:

@XmlRootElement(name = "algun_nombre") 

This way in the get of the field that you do not want to see you can write it down with:

@XmlTransient
    
answered by 17.03.2017 в 20:34