Here are 3 simple Java classes:
#Getter
#Setter
public class Animal {
protected String name;
protected Integer age;
public enum AnimalType{
ANGRY,
FUNNY
}
}
#Getter
#Setter
public class Cat extends Animal{
private AnimalType type=AnimalType.ANGRY;
}
#Setter
#Getter
public class Dog extends Animal{
private AnimalType type=AnimalType.FUNNY;
}
What do I need to do so that swagger generates something like this?
type* AnimalType string
value= FANNY
Enum:
>Array[2]
Is there any way to do it? I need to add ENUM for all classes and specific value of this enum for each class.
Related
I would like to fill this fields with mapstruct but i don't know how to fix it because i never use mapstruct with generic type thanks for any attentions
#Data
#ToString
public class SvGateRequest<T> {
public SvGateRequest(String method, T params) {
this.method = method;
this.params = params;
}
private String jsonrpc="2.0";
private String method;
private Long id = System.currentTimeMillis();
private T params;
#Data
#ToString
#Builder
#DynamicUpdate
#DynamicInsert
public static class CardNew{
private Card card;
private CardVerify otp;
#Data
#ToString
#Builder
public static class Card{
private String pan;
private String expiry;
}
#Data
#ToString
#Builder
public static class CardVerify{
private Long id;
private String code;
}
}
}
it is my dto class which take params to fill FillCard class but sometimes instead of FillCard class i can use fill CardVerify class with another dto class thats why i try optimize my code
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
public class FillCard {
private String pan;
private String expiry;
}
whether it is preferable to use generic types with mapstruct itself or not
#Data
#Builder
public class ClassA {
private ClassB b;
private String createdBy;
private class ClassB {
String name;
int version;
}
}
Hi I want to create ClassA in another java file this way:
ClassA.builder().createdBy("Alex")
.b(ClassB.builder()
.name("Game")
.version(2).build())
.build();
Is this possible for private class classB?
Thx
I recommend as below:
#Data
#Builder
public class ClassA {
private ClassB b;
private String createdBy;
#Builder
#Data
static class ClassB {
String name;
int version;
}
}
if you want unvisible ClassB it is impossible. and To using #Builder on inner class , class must be static class
private modifier means that the variable is only visible for this class only. So if you create private class B inside class A it means B only visible for A.
As #sweeper said in the comment, you cannot use class B in another file / class except for class A.
If you still want to use that way, you can create class B independently.
ClassA.class
#Data
#Builder
public class ClassA {
private ClassB b;
private String createdBy;
}
ClassB.class
#Data
#Builder
class ClassB {
String name;
int version;
}
I am not sure if title of the post is understandable, but I will try to explain it with examples.
The main class is the entity, lets name it Animal
#Entity
public class Animal {
#Id
private Long id;
private String param1;
}
The param1 is some basic property which only extended classes will expose what it represents:
public class Cat extends Animal {
public String getName() {
return super.getParam1();
}
public void setName(String name) {
super.setParam1(name);
}
}
public class Dog extends Animal {
public String getBreed() {
return super.getParam1();
}
public void setBreed(String breed) {
super.setParam1(breed);
}
}
When creating a new object of Cat and trying to save it, I get the error Unknown entity: com.example.Cat.
I have read about persistence inheritance but I think this is not the case since mine extended classes are just some logic in the services and not the actual tables in the model.
Is there any solution on how I can save the superclass ?
Let's have example base class
#Data
#NoArgsConstructor
#AllArgsConstructor
public class User {
#JsonProperty("login")
private String login;
#JsonProperty("password")
private String password;
#JsonProperty("additionalData")
private String additionalData;
}
and second one that extends User class
#Data
#NoArgsConstructor
#AllArgsConstructor
public class EnhancedUser extends User {
#NotNull
#JsonProperty("additionalData")
private String additionalData;
}
I but It doesn't work because when I create instance of EnhancedUser class field additionalData can be null.
Any idea?
Look:
public class Sample {
public static void main(String[] args) {
EnhancedUser enhancedUser = new EnhancedUser();
enhancedUser.setAdditionalData("TAMU");
enhancedUser.setLogin("ANY");
enhancedUser.setPassword("ANY");
System.out.println(enhancedUser);
System.out.println(enhancedUser.getAdditionalData());
}
#Data
#NoArgsConstructor
#AllArgsConstructor
public static class User {
private String login;
private String password;
private String additionalData;
}
#EqualsAndHashCode(callSuper = true)
#Data
#NoArgsConstructor
#AllArgsConstructor
#ToString(callSuper = true)
public static class EnhancedUser extends User {
#NotNull
private String additionalData;
}
}
And the result of printlnis
Sample.EnhancedUser(super=Sample.User(login=ANY, password=ANY, additionalData=TAMU), additionalData=TAMU)
TAMU
You do realize that you actually have 2 fields "additionalData"? Since you can't override fields but merely hide them. And this is a huge nono anti-pattern in general.
Either you rename your field or you think of a more approriate implementation, like implementing this logic yourself with and a constructor argument and a call of the additionalData setter from your constructor.
I have classes in following structure :
class Member {
public long id;
public String name;
public String type;
public Pet pet;
};
public abstract class Pet {
}
public CatPet extends Pet {
public int age;
public String color;
}
public DogPet extends Pet {
public int age;
public String breed;
}
I have to serialize the objects of class Member into JSON string and vice-versa. Can I somehow make the serialization such that serialization of object Pet in class Member will be dependent on member 'type'. If type = "cat" it should serialize/deserialize using class CatPet.
Yes, you typically use annotation #JsonTypeInfo on base class, to indicate how polymorphic type information is to be used. And with that, things will "just work".