Improving Java Backend Code: Bean Conversion, DTO Handling, Lombok, Validation, and Refactoring Practices
This article provides comprehensive guidance on Java backend development, covering IDE selection, bean and DTO conversion techniques, validation with JSR‑303, extensive Lombok usage, builder patterns, proxy design, refactoring strategies, and the balance between business‑driven and technology‑driven development.
The article begins by stating that it is not a boastful piece but focuses on fundamental Java issues, recommending IntelliJ IDEA over Eclipse for a smoother development experience.
It then discusses the importance of proper package naming, suggesting the use of com.xxx.entity instead of com.xxx.domain for database‑mapped objects, and introduces DTOs as transfer objects for API communication.
Conversion examples are provided, showing manual property copying and recommending org.springframework.beans.BeanUtils.copyProperties to simplify the process. The author emphasizes extracting conversion logic into separate methods for better semantics, referencing Martin Fowler's "Extract Method" refactoring.
An abstract conversion interface is defined: public interface DTOConvert { T convert(S s); } Implementations of this interface are shown, followed by a more sophisticated approach using Guava's Converter class to support forward and backward conversions, with an example that throws an AssertionError for unsupported reverse conversion. Validation is addressed using JSR‑303 annotations ( @NotNull ) and Spring MVC's @Valid with BindingResult , advising that validation errors be transformed into API‑level exceptions. The article then advocates embracing Lombok to reduce boilerplate, demonstrating annotations such as @Setter , @Getter , @Data , @AllArgsConstructor , @NoArgsConstructor , @Accessors(chain = true) , and @Builder . Examples show how Lombok enables chainable setters, static factory methods, and builder patterns without verbose code. Static construction utilities from Guava ( Lists.newArrayList() , Maps.newHashMap() ) are highlighted as more expressive alternatives to direct new calls. The author introduces a proxy pattern for RestTemplate , first implementing a FilterRestTemplate that delegates all RestOperations methods, then simplifying it with Lombok's @Delegate annotation: @AllArgsConstructor public abstract class FilterRestTemplate implements RestOperations { @Delegate protected volatile RestTemplate restTemplate; } Refactoring principles are emphasized throughout, encouraging developers to think before coding, use appropriate libraries (e.g., Joda‑Time or Java 8's java.time ), and apply clean‑code practices. Design‑pattern discussions clarify that patterns are tools, not status symbols, and advise choosing patterns based on business needs rather than over‑engineering. The balance between business‑driven and technology‑driven development is explored, urging developers to align technical choices with project goals. Finally, the article lists essential skills for Java developers: UML class and sequence diagrams, clean‑code reading (e.g., Robert C. Martin's book), coding standards, and basic Linux command proficiency.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.