Complex Bean Mapping

jakarta-ee, java, java-6, javabeans

Solution

You can use basically AOP (Aspect Oriented Programming) with Spring. On spring you can create a proxy object with extra information you need. Good starting point is: http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/

Official page on Aspect Oriented Programming: http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html

This example/answer can be useful: Intercepting method with Spring AOP using only annotations

Problem

I am trying to find the best solution for a problem I have with mapping a simple bean structure that is being sent to a browser-based JavaScript application. The current requirement is to manage most of the display control on the old Java backend. Currently we have a service style layer that is producing value objects with no display logic built into them like: ``` public class Example1 { String value1; Boolean value2; Example3 value3; public String getValue1(){...} public void setValue1(){...} .... } ``` My goal is to be able to map a generic structure over all fields such that it adds the new display structure that is required by the front-end. I would like to manage only the original structure class (Example1 class) structure and simply set the extra values in a wrapper to the old service layer. The generic structure would take the form of the following class: ``` public class Presentable<T> { T value; boolean visible = true; boolean mandatory = false; List<String> errors = new ArrayList<>(); public T getValue() {...} public void setValue(T value) {...} ... } ``` The end result would look something like the following, where value is equal to the value in the original structure: ``` public class Example2{ Presentable<String> value1; Presentable<Boolean> value2; Presentable<Example3> value3; public Presentable<String> getValue1(){...} public void setValue1(){...} ... } ``` Is there a solution to this problem without writing an Example2 style class and copying in every single value? I am open to modification to the Example1 class as it does not affect consumers of the old service. Thanks.

Original source

Related problems