title: How do I design a generic Response builder / RESTful Web Service using Spring MVC?
body: <p>Trying to build a RESTful web service using Spring MVC. </p>
  
  <p>The controller should return specific Java types, but the response body must be a generic envelope.  How can this be done?  </p>
  
  <p>The following sections of code are what I have so far:</p>
  
  <p>Controller method:</p>
  
  <pre><code>    @Controller
      @RequestMapping(value = "/mycontroller")
      public class MyController {
  
          public ServiceDetails getServiceDetails() {
               return new ServiceDetails("MyService");
          }
      }
  </code></pre>
  
  <p>Response envelope:</p>
  
  <pre><code>    public class Response&lt;T&gt; {
  
          private String message;
          private T responseBody;
  
      }
  </code></pre>
  
  <p><code>ServiceDetails</code> code:</p>
  
  <pre><code>    public class ServiceDetails {
  
          private String serviceName;
  
          public ServiceDetails(String serviceName) {
              this.serviceName = serviceName;
          }
      }
  </code></pre>
  
  <p>Intended final response to clients should appear as:</p>
  
  <pre><code>   {
  
       "message" : "Operation OK"
       "responseBody" : {
                          "serviceName" : "MyService"
                        }
  
     }  
  </code></pre>