How to override Lombok Setter methods - Java
文章推薦指數: 80 %
I'm using lombok in my project and generation Setters and Getters using @Setters and @Getters annotations on top of POJO class. I'm trying to override ... Questions Home Questions HowtooverrideLombokSettermethods Tags:java,lombok I’musinglombokinmyprojectandgenerationSettersandGettersusing@[email protected]’mtryingtooverridesettersmethodofapropertybutit’snotworking IwanttocheckifJSONpropertyisEmptyorNulliwanttosetdefaultvalueinSettermethod @Setter @Getter @NoArgsConstructor @AllArgsConstructor @Accessors(chain=true) @ToString publicclassDefaultModel{ privateStringname; @Setter(AccessLevel.NONE)privateStringage; publicvoidsetAge(Stringage){ if(age==null||age.trim().isEmpty()||age.equals("null")){ this.age="10"; }else{ this.age=age; } } } Workingscenarios: { "name":"some", "age":null } { "name":"some", "age":"null" } { "name":"some", "age":"" } FailedScenario: { "name":"some" } Output: DefaultModel(name=some,age=null) Andi’mfollowingthisasreferencealsohere,butnolucksofar Answer EitheryoujusthitabugI’veneverseenoryou’retestingitwrong. Anannotationlike @Setter(AccessLevel.NONE)privateStringage; onthefieldlevelindeedstopsthesetterfrombeinggenerated.Butgiventhatyou’redefiningasetter,youdon’tevenneedit.Anexplicit@Setterstopsthegeneration,too. I’vejusttriedyourexampleusingEclipse4.7.3aandLombok1.18.0andyour(buggy)settergetscalled.I’vebeenusingLombokalotoverafewyearsandneverencounteredsuchabug. MostprobablytheproblemisthatyourJSONdeserializerdoesnotusesettersatall.Iguess,you’retestingsomethinglike DefaultModeldefaultModel=deserialize("{"name":"some"}",DefaultModel.class); insteadoftestingthesetterdirectly.Andthat’stheproblem. PrevConvertLongtoDateTimefromC#DatetoJavaDateNextIncompatibleJavaVersionsforWindowBuilder Source:stackoverflow Search RecentQuestions implementationofGatewayGlobalfilterinKotlintoJavaimplementation DownCastingingenericsinjava SwaggerisnotworkinginSpringbootframework[closed] Cannotreadthearraylengthbecause“”isnull ShouldEntityManagerFactorybeclosedatapplicationshutdown? Tags algorithm amazon-web-services android android-studio arraylist arrays collections concurrency eclipse file firebase firebase-realtime-database generics gradle hibernate intellij-idea jackson jar java java-8 java-stream javafx jdbc jpa json junit kotlin maven multithreading mysql oop regex rest selenium spring spring-boot spring-data-jpa spring-mvc spring-security sql string swing tomcat unit-testing xml
延伸文章資訊
- 1Lombok how to customise getter for Boolean object field?
You have two options to override a Getter/Setter in your class. One is the answer from First Opti...
- 2java - not - lombok override getter - Code Examples
java - not - lombok override getter. @JsonIgnore with @Getter Annotation (3). Recently i had the ...
- 3Omitting Getter or Setter in Lombok | Baeldung
This access level lets us override the behavior of a @Getter, @Setter, or @Data annotation on a c...
- 4Lombok @Getter @Setter and lazy getters examples
This lets you override the behavior of a @Getter , @Setter or @Data annotation on a class. Lobmok...
- 5How to override Lombok Setter methods - Java
I'm using lombok in my project and generation Setters and Getters using @Setters and @Getters ann...