dto建模类中的java简化锅炉板代码(getter和setter)

xkftehaa  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(340)

我想删除下面的getters和setters样板代码,但我不确定如何删除。我有一个dto类来为json列建模 questionVariables 在我的实体里。
dto公司

import java.io.Serializable;

public class QuestionVariables implements Serializable  {

    private ScreenOptions screenOptions;
    private QuestionOptions questionOptions;

    public ScreenOptions getScreenOptions() {
        return this.screenOptions;
    }

    public void setScreenOptions(ScreenOptions screenOptions) {
        this.screenOptions = screenOptions;
    }

    public static class ScreenOptions implements Serializable {

        private boolean isSkippable;

        public boolean getisSkippable() {
            return this.isSkippable;
        }

        public void setisSkippable(boolean isSkippable) {
            this.isSkippable = isSkippable;
        }
    }

    public static class QuestionOptions implements Serializable {

    }
}

实体

import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Data;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

@TypeDefs({ @TypeDef(name = "json", typeClass = JsonStringType.class) })
@Entity
@Table(name = "questions")

@Data
public class Question {

    @JsonSerialize
    @JsonDeserialize
    @Nullable
    @Type(type = "json")
    @Column(columnDefinition = "json", nullable = true)
    private QuestionVariables questionVariables;
}

我试过使用lombok@getter和@setter注解,但都不起作用。我得到下面的错误

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@NoArgsConstructor
@Getter
@Setter
public class QuestionVariables implements Serializable  {

    @NoArgsConstructor
    @Getter
    @Setter
    public static class ScreenOptions implements Serializable {
        private boolean isSkippable;
    }

    @NoArgsConstructor
    @Getter
    @Setter
    public static class QuestionOptions implements Serializable {

    }
}

错误

[ERROR] 2021-04-07 10:17:52,484 com.nana.elp.utils.exceptions.DefaultExceptionHandlerAdvice http-nio-8000-exec-2 - Error Code {unknown}, Error Message : java.lang.IllegalArgumentException: The given string value: {
  "screenOptions" : {
    "isSkippable": true
  }
} cannot be transformed to Json object; nested exception is org.hibernate.HibernateException: java.lang.IllegalArgumentException: The given string value: {
  "screenOptions" : {
    "isSkippable": true
  }
} cannot be transformed to Json object
nnvyjq4y

nnvyjq4y1#

我很少使用lombok,但我认为应该在属性上使用@getter和@setter注解,而不是类

public class ScreenOptions {
    @Getter @Setter private boolean isSkippable = false;
}
r8uurelv

r8uurelv2#

@Getter
@Setter
public class QuestionVariables implements Serializable  
{
    private ScreenOptions screenOptions;
    private QuestionOptions questionOptions;
}

@Getter
@Setter
public static class ScreenOptions implements Serializable 
{
    private boolean isSkippable;
}

@Getter
@Setter
public static class QuestionOptions implements Serializable 
{
     ...
}
6ojccjat

6ojccjat3#

我不得不补充一句 @JsonDeserializeisSkippable it工作的财产

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

public class QuestionVariables implements Serializable  {

    @Getter @Setter private ScreenOptions screenOptions;

    @Getter
    @Setter
    public static class ScreenOptions implements Serializable {
        @JsonDeserialize private boolean isSkippable;
    }
}

相关问题