title: How to convert object of any class to a Map in Java?
body: <p>In Spring MVC we have to use <code>org.springframework.ui.Model</code> instance to pass model to a view. It is not strongly-typed and we have to dynamically build the object like this:</p>
  
  <pre><code>model.addAttribute("departmentID", departmentID);
  model.addAttribute("departmentName", departmentName);
  model.addAttribute("employees", employees);
  </code></pre>
  
  <p>However, I came from ASP.NET MVC, where I passed strongly-typed objects to a view, and I had <code>ViewDepartment</code> class which had <code>departmentID</code>, <code>departmentName</code> and <code>employees</code> fields, and I simply passed instance to a view. Here it doesn't work, but I still need to use <code>ViewDepartment</code> class, because I occasionally send it as response to AJAX-requests.</p>
  
  <p>So, to get this working in Spring MVC, I need to translate <code>ViewDepartment</code> object to instance of <code>org.springframework.ui.Model</code>, one way is to build <code>org.springframework.ui.Model</code> from <code>HashMap</code>: </p>
  
  <pre><code>Model.addAllAttributes(Map&lt;String,?&gt; attributes) 
  </code></pre>
  
  <p>The question is, <strong>how to build <code>Map&lt;String,?&gt; attributes</code> from instance of <code>ViewDepartment</code> class</strong>? Creating HashMap object and manually setting each attribute from each property is not option, because it`s not DRY. I need some way to do this with any class, because I have other model classes in other controllers.</p>
  
  <p>Or, may be, someone can tell another solution to this task, related to Spring MVC specifically.</p>