Sending numeric value of an Enum by HTTP request in Java

Asked

Viewed 212 times

0

I have a problem in my system, in my back-end in Java com Spring Boot I have a class that has a property that’s kind of a Enum that I created, and my Front-end which is in Angular when making a request to return a value of the class type the value that is in the property of the Enum is not the numerical value of Enum and yes the name.

Example, I have a Enum Status.enum that has the values ATIVO(1) and DESATIVO(2), when I send this one Enum by HTTP request it ta returning: { "status": "ATIVO" }. And the right thing would be to return the following request: { "status": 1 }. I’ve tried to overwrite the toString() for it to return the numeric value in String but not even this could.

Meu Enum:

public enum DiaSemanaEnum {

    SEGUNDA(1),
    TERCA(2),
    QUARTA(3),
    QUINTA(4),
    SEXTA(5),
    SABADO(6),
    DOMINGO(7);

    private Integer codigo;

    DiaSemanaEnum(Integer codigo) {
        this.codigo = codigo;
    }

    public Integer getCodigo() {
        return codigo;
    }

    public static DiaSemanaEnum getObjetoEnum(Integer codigo) {

        for (DiaSemanaEnum dse : DiaSemanaEnum.values()) {
            if (dse.getCodigo().equals(codigo)) {
                return dse;
            }
        }

        throw new IllegalArgumentException();
    }

    public static DiaSemanaEnum getObjetoEnum(String nome) {

        for (DiaSemanaEnum dse : DiaSemanaEnum.values()) {
            if (dse.name().equals(nome)) {
                return dse;
            }
        }

        throw new IllegalArgumentException();
    }

}

My class that has the Enum as one of the properties:

@Getter
@Setter
public class ConfiguracaoAgendaProfissional {

    private Long id;

    @JsonIgnore
    private Profissional profissional;

    private DiaSemanaEnum diaSemana;

    private String descricao;

    private LocalTime horaInicio;

    private LocalTime horaFim;

    private List<ConfiguracaoAgendaProfissionalIntervalo> intervalos = null;


    public ConfiguracaoAgendaProfissional(){

    }

    @JsonCreator
    public ConfiguracaoAgendaProfissional(@JsonProperty("id") Long id, @JsonProperty("diaSemana") Integer diaSemana,
                                      @JsonProperty("descricao") String descricao, @JsonProperty("horaInicio") LocalTime horaInicio,
                                      @JsonProperty("horaFim") LocalTime horaFim) {
        this.id = id;
        this.diaSemana = diaSemana != null ? DiaSemanaEnum.getObjetoEnum(diaSemana) : null;
        this.descricao = descricao;
        this.horaInicio = horaInicio;
        this.horaFim = horaFim;
    }

    public ConfiguracaoAgendaProfissional(Long id, Profissional profissional, Integer diaSemana, String descricao,
                                      LocalTime horaInicio, LocalTime horaFim) {

        this.id = id;
        this.profissional = profissional;
        this.diaSemana = DiaSemanaEnum.getObjetoEnum(diaSemana);
        this.descricao = descricao;
        this.horaInicio = horaInicio;
        this.horaFim = horaFim;
    }

    public ConfiguracaoAgendaProfissional(Long id) {
        this.id = id;
    }

}

Before I was having a problem to send a value of the Enum type from the Front-end to the Back-end and I managed to solve with the @JsonCreator and the @JsonProperty but group my problem is contrary, because I want to send a certain value of the Enum type to the Front-end.

Someone knows how to help me?

  • 1

    Post your Java and Enum class code

  • I don’t see the need for this because it’s a very general question but I’ll post it anyway.

1 answer

1

I managed to find a "solution", in Enum put the following note:

import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(shape = JsonFormat.Shape.NUMBER)
public enum DiaSemanaEnum { .....

this returns the numeric value by JSON, only in my case I put the values (for example) LIVRE(1), OCUPADO(2), only that it is returning not the value I put in the Enum but rather his position, like, the LIVRE(1) is not returning 1, but 0 because it is his position. That was the only problem I found, rest ta all deboa.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.