add description to each enum value for swagger-UI documentation - java

How can I add some description to each enum value for swagger-UI documentation?
My EnumClass:
#ApiModel
public enum EnumCarrierSelectionSubstitutionInformation {
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_1(1), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_2(2), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_3(3), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_4(4), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_5(5), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_6(6), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_7(7);
private int numVal;
EnumCarrierSelectionSubstitutionInformation(int numVal) {
this.numVal = numVal;
}
public int getNumVal() {
return numVal;
}
}
The model
private EnumCarrierSelectionSubstitutionInformation carrierSelectionSubstitutionInformation;
// getter setter......
I would like to add some description to CARRIER_SELECTION_SUBSTITUTION_INFORMATION_1.
I tried
#ApiModelProperty(value = "blabla2")
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_1(1), //
but that is not working.
Swagger-UI output:
carrierSelectionSubstitutionInformation (string, optional) = ['CARRIER_SELECTION_SUBSTITUTION_INFORMATION_1', 'CARRIER_SELECTION_SUBSTITUTION_INFORMATION_2', 'CARRIER_SELECTION_SUBSTITUTION_INFORMATION_3', 'CARRIER_SELECTION_SUBSTITUTION_INFORMATION_4', 'CARRIER_SELECTION_SUBSTITUTION_INFORMATION_5', 'CARRIER_SELECTION_SUBSTITUTION_INFORMATION_6', 'CARRIER_SELECTION_SUBSTITUTION_INFORMATION_7']
string
Enum: "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_1", "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_2", "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_3", "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_4", "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_5", "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_6", "CARRIER_SELECTION_SUBSTITUTION_INFORMATION_7"
How can I do that?

public class Enum {
public enum EnumCarrierSelectionSubstitutionInformation {
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_1(1,"ONE"), //
CARRIER_SELECTION_SUBSTITUTION_INFORMATION_2(2,"TWO"),
private final int value;
private final String description;
EnumCarrierSelectionSubstitutionInformation(int value, String desc){
this.value = value;
this.description = desc;
}
public int value(){
return this.value;
}
public String description(){
return this.description;
}
}
public static void main(String[] args){
for (EnumCarrierSelectionSubstitutionInformation elem: EnumCarrierSelectionSubstitutionInformation.values()){
System.out.println(elem + "value is "+ new Integer(elem.value()) + " desc is " + elem.description());
}
}
}

Related

JSON serialize to a different value

I have the below json which im serializing
{
"name":"John",
"switch":"1"
},
{
"name":"Jim",
"switch":"0"
}
I want to serialize it to a differnt name So I had to do it like below
class Data {
private String name;
private String flag;
#JsonProperty("flag")
public byte getFlag() {
return flag;
}
#JsonProperty("switch")
public void setSwitch(String s) {
this.flag = flag;
}
}
So that I get it converted as below
{
"name":"John",
"flag":"1"
},
{
"name":"Jim",
"flag":"0"
}
Now I wanted to map the numic values to Y and N for 1 and 0 respectively. Can I acheive that ?
Im expecting my final string to be like this
{
"name":"John",
"switch":"Y"
},
{
"name":"Jim",
"switch":"N"
}
I agree with #Gaƫl J, but still if you want to go ahead this code change might help you to convert that 1/0 to Y/N.
public class Application {
#ToString
static class Input {
#JsonProperty("name")
private String name;
#JsonProperty("switch")
private String flag;
#JsonProperty("name")
public void setName(String name){
this.name = name;
}
#JsonProperty("switch")
public void setSwitch(String s) {
for(SwitchMap valuePair : SwitchMap.values()){
if(valuePair.getValue().equals(s)){
this.flag = valuePair.name();
}
}
}
}
public static void main(String[] args) throws JsonProcessingException {
String json = "{\n" +
"\"name\":\"John\",\n" +
"\"switch\":\"1\"\n" +
"}";
ObjectMapper mapper = new ObjectMapper();
Input in = mapper.readValue(json, Input.class);
System.out.println(mapper.writeValueAsString(in));
}
}
define an enum with the mapping
#Getter
public enum SwitchMap {
Y("1"),
N("0");
private final String value;
private SwitchMap(String value){
this.value = value;
}
}

Superclass overriding Subclass with default values from constructor in Java

I have an assignment and my superclass default values always override the values I pass in the Test main method. In the debugger, i see the passing of the productNumber(1234) and productTitle("Daughter"), but then it's overridden with the default values. Any thoughts, i keep making minor changes, checking for changes, still the same results.
Product Superclass
public abstract class Product {
private int productNumber;
private String productTitle;
//Two constructors required
public Product(){
productNumber = 0;
productTitle = "";
}
public Product(int productNumber, String productTitle) {
this.productNumber = productNumber;
this.productTitle = productTitle;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
public int getProductNumber() {
return productNumber;
}
public void setProductTitle(String productTitle) {
this.productTitle = productTitle;
}
public String getProductTitle() {
return productTitle;
}
//Override toString() required
#Override
public String toString() {
return productNumber + " " + productTitle;
}
// Required Product class declares abstract method with this signature: public String getDisplayText()
public abstract String getDisplayText();
//Override equals() required
#Override
public boolean equals(Object object) {
if (object instanceof Product) {
Product product2 = (Product) object;
if (productNumber == (product2.getProductNumber()) &&
productTitle.equals(product2.getProductTitle())){
return true;
}
}
return false;
}
}
Music Subclass extends Product Superclass
public class Music extends Product {
private String artist;
private String style;
private String medium;
public Music() {
super();
artist = "";
style = "";
medium = "";
}
public Music(int productNumber, String productTitle, String artist, String style, String medium) {
super();
this.artist = artist;
this.style = style;
this.medium = medium;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
#Override
public String getDisplayText() {
return super.toString() + " by " + artist + " " + style + " " + medium;
}
#Override
public boolean equals(Object object){
if (object instanceof Music){
Music m = (Music) object;
if (artist.equals(m.getArtist()) &&
style.equals(m.getStyle()) &&
medium.equals(m.getMedium())){
return true;
}
}
return false;
}
}
Print String
public class Test {
public static void main(String[] args) {
// Expected result: 1234 Daughter by Pearljam Alternative online
Music music1 = new Music(1234,"Daughter", "Pearljam","Alternative","online");
System.out.println(music1.getDisplayText());
}
}
you are not passing values from subclass to your parentclass
instead of super() you need to do below -
super(productNumber,productTitle);
update needed in your code :-
public Music(int productNumber, String productTitle, String artist, String style, String medium) {
super(productNumber,productTitle);
this.artist = artist;
this.style = style;
this.medium = medium;
}
You need to pass productNumber and productTitle in the super(..., ...) call inside the Music constructor up to the parent class.
You need to invoke
super(productNumber, productTitle)
inside the Music constructor to pass the parameters to its parent.

Can I move overriden methods in Enum to a class

I have an Enum which has three constants as of now. All three are having 10+ overridden methods. Is it possible to have a better design so that I can put all of them to a common class.
Updated with sample code:
public enum MyEnum {
FIRST {
#Override
public String doIt() {
return "1: " + someField; //error
}
#Override
public String getCategory() {
return "MyCategory1"; //error
}
},
SECOND {
#Override
public String doIt() {
return "2: " + someField; //error
}
#Override
public String getCategory() {
return "MyCategory2"; //error
}
},
THIRD {
#Override
public String doIt() {
return "3: " + someField; //error
}
#Override
public String getCategory() {
return "MyCategory3"; //error
}
};
private String someField;
public abstract String doIt();
public abstract String getCategory();
}
Why not try it like this:
public enum MyEnum{
FIRST(1),
SECOND(2),
THIRD(3);
private final int value;
private MyEnum(int value) {
this.value = value;
}
public String doIt() {
return value + ": " + someField;
}
public String getCategory() {
return "MyCategory" + value;
}
}
There is no need to repeat any methods, let alone to override them.
If you want specifically a class:
import lombok.AllArgsConstructor;
import lombok.Getter;
#Getter
#AllArgsConstructor
public class Category {
public static final Category FIRST = new Category(1, "MyCategory1", "someField");
public static final Category SECOND = new Category(2, "MyCategory2", "someField");
public static final Category THIRD = new Category(3, "MyCategory3", "someField");
private int categoryId;
private String category;
private String someField;
public String doIt() {
return this.categoryId +": " + this.someField;
}
}
And then just use constants:
public class TestCategory {
public static void main(String[] args) {
System.out.println(FIRST.doIt());
System.out.println(FIRST.getCategory());
}
}

writing multiple objects to parcel

I am trying to save an enum 'Status' into a custom class that implements parcelable. I have found online how I can save Strings, ints or enums in one class that implements parcelable, but not how I can save these three things all at once. I am sorry if the solution is obvious, but I just can't figure it out.
Here is what my enum looks like:
public enum Status {
INITIALIZED, UPDATED, DELETED
}
And this is what I have so far:
public class Recipe implements Parcelable{
private String id;//this should be an int, same problem
private String recipeName;
private String recipePreperation;
private Status status;
private final static int MAX_PREVIEW = 50;
public Recipe(int parId, String parRecipeName, String parRecipePreperation) {
this.id = "" + parId;
this.recipeName = parRecipeName;
this.recipePreperation = parRecipePreperation;
this.status = Status.INITIALIZED;
}
public Recipe(Parcel in){
String[] data = new String[4];
in.readStringArray(data);
this.id = data [0];
this.recipeName = data[1];
this.recipePreperation = data[2];
this.status = data[3];//what I intend to do, I know this is wrong
}
public int GetId() {
return Integer.parseInt(id);
}
public String GetRecipeName() {
return this.recipeName;
}
public void SetRecipeName(String parRecipeName) {
this.recipeName = parRecipeName;
}
public String GetRecipePreperation() {
return this.recipePreperation;
}
public void SetRecipePreperation(String parRecipePreperation) {
this.recipePreperation = parRecipePreperation;
}
public Status GetStatus() {
return this.status;
}
public void SetStatus(Status parStatus) {
this.status = parStatus;
}
public String toString() {
String recipe = this.recipeName + "\n" + this.recipePreperation;
String returnString;
int maxLength = MAX_PREVIEW;
if (recipe.length() > maxLength) {
returnString = recipe.substring(0, maxLength - 3) + "...";
} else {
returnString = recipe;
}
return returnString;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int arg1) {
dest.writeStringArray(new String [] {
this.id,
this.recipeName,
this.recipePreperation,
this.status//what I intend to do, I know this is wrong
});
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Recipe createFromParcel(Parcel in) {
return new Recipe(in);
}
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
}
How do I save an int, an array of strings and an enum into a class that implements the parcelable, so it can writeToParcel()?
There's no need to read and write to/from string array. Just write each string and finally the status as Serializable. This is how I fix it.
public Recipe(Parcel in){
this.id = in.readString();
this.recipeName = in.readString();
this.recipePreperation = in.readString();
this.status = (Status) in.readSerializable();
}
public void writeToParcel(Parcel dest, int arg1) {
dest.writeString(this.id);
dest.writeString(this.recipeName);
dest.writeString(this.recipePreperation);
dest.writeSerializable(this.status);
}

Java enum: get FieldName knowing its value

public enum Code {
E1330("MERCOSUR (SOUTH AMERICAN COMMON MARKET)"),
E0257("Guinea Biss."),
E0252("Gambia");
private Code(String value){
setStringValue(value);
}
private Code (int value) {
setIntValue(value);
}
private int intValue;
private String stringValue;
public String getStringValue() {
return stringValue;
}
public int getIntValue() {
return intValue;
}
public void setStringValue(String value) {
this.stringValue = value;
}
public void setIntValue(int value) {
this.intValue = value;
}
}
How can I get the field name of the Code whose value is "Gambia"?
-> it would be E0252
Thank you
You can search for it via a loop:
String val = "Gambia";
String field = "";
for (Code c : Code.values())
if (c.getStringValue().equals(val)) {
field = c.name();
break;
}
System.out.println(field);
Output:
E0252

Categories

Resources