Dada uma classe Property
que possui um Enumerate
chamado PropertyType
para identificar seu tipo, se é propriedade do sistema, ou do ambiente, ou mesmo se é configuração da aplicação. O retorno default do json
é o nome do tipo:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Property { | |
private PropertyType type; | |
public PropertyType getType() { | |
return type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PropertyType { | |
SYSTEM_PROPERTY(0), SYSTEM_ENVYRONMENT(1) ,APPLICATION_CONFIGURATION(2); | |
private int value; | |
PropertyType(int value) { | |
this.value = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"type": "SYSTEM_PROPERTY"} |
Podemos capturar o valor numérico do tipo e retorná-lo através da annotation @JsonFormat
:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Property { | |
private PropertyType type; | |
@JsonFormat(shape = JsonFormat.Shape.NUMBER_FLOAT) | |
public PropertyType getType() { | |
return type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PropertyType { | |
SYSTEM_PROPERTY(0), SYSTEM_ENVYRONMENT(1) ,APPLICATION_CONFIGURATION(2); | |
private int value; | |
PropertyType(int value) { | |
this.value = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"type": 0} |
Se tivéssemos em nosso Enum mais valores descritivos para cada tipo, poderiamos retornar o objeto inteiro:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Property { | |
private PropertyType type; | |
@JsonFormat(shape = JsonFormat.Shape.OBJECT) | |
public PropertyType getType() { | |
return type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PropertyType { | |
SYSTEM_PROPERTY(0, "Propriedades do sistema"), SYSTEM_ENVYRONMENT(1, "Propriedades do ambiente"), APPLICATION_CONFIGURATION(2, "Configuração da aplicaçaõ"); | |
private int codigo; | |
private String descricao; | |
PropertyType(int codigo, String descricao) { | |
this.codigo = codigo; | |
this.descricao = descricao; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"type": {"codigo": 0,"descricao":"Propriedades do sistema"}} |
Ou poderíamos retornar apenas um dos valores através da annotation @JsonValue
no método get:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Property { | |
private PropertyType type; | |
public PropertyType getType() { | |
return type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PropertyType { | |
SYSTEM_PROPERTY(0, "Propriedades do sistema"), SYSTEM_ENVYRONMENT(1, "Propriedades do ambiente"), APPLICATION_CONFIGURATION(2, "Configuração da aplicaçaõ"); | |
private int codigo; | |
private String descricao; | |
PropertyType(int codigo, String descricao) { | |
this.codigo = codigo; | |
this.descricao = descricao; | |
} | |
//… | |
@JsonValue | |
public String getDescricao() { | |
return descricao; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"type": "Propriedades do sistema"} |