Why Spring BeanUtils Fails to Copy Static Inner Classes and How to Fix It
In a recent Java backend project, using Spring's BeanUtils.copyProperties to copy a request object with static inner classes and List fields caused missing fields and request failures, revealing the need for proper getters, setters, and separate handling of inner‑class properties.
Background
In a recent project we needed to call a third‑party API. The SDK’s Request class had a serialization bug, so we rewrote a Request class with the same fields, but it contained a static inner class and two List fields.
Problem
We used Spring’s BeanUtils.copyProperties to copy the fields from our AddRequest to the SDK’s MixAddRequest. The request failed because a required field inside the Ticket inner class was missing; the copy method did not transfer the inner‑class instance.
Investigation
We created two demo classes ( CopyTest1 and CopyTest2) each with a static inner class and a List of that inner class. After adding Lombok’s @Data and @ToString, the simple fields were copied, but the inner‑class instance remained null.
Root Cause
BeanUtils requires getter and setter methods for every property; without them the copy fails.
If two classes have inner classes with the same name but belong to different outer classes, BeanUtils treats them as different types and does not copy them.
Java generics are erased at runtime, so a List<Order> becomes a raw List. The SDK accepted the list because it only needed the JSON representation.
The parameter order of Spring’s BeanUtils.copyProperties is opposite to Apache’s version, so importing the wrong method can cause silent failures.
Solution
Copy the inner‑class property separately, or write a recursive copy method for beans that contain nested objects. Ensure all inner‑class fields have setters, and remember the correct parameter order when calling BeanUtils.copyProperties.
After adding a second copyProperties call for the inner class, the request succeeded.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack 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.
