How to override Lombok Setter methods - Java

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?