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?
Post your Java and Enum class code
– Costamilam
I don’t see the need for this because it’s a very general question but I’ll post it anyway.
– Bruno Eduardo