title: Spring Controller always produce json
body: <p>Is there anyway to force spring to always produce json, even an empty json object if there's no data to return.
  Our services go through another service that rejects any response that isn't valid json (regardless of status code). It's not nice but we have no control of this. </p>
  
  <p>With spring controllers you can tell them to produce json, but this only works when there's content to return. Is there a quick and elegant way to make all responses be json?</p>
  
  <pre><code>@RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public
  @ResponseBody
  ResponseEntity&lt;String&gt; test(){
      // if this returns null or an empty string the response body will be emtpy 
      // and the content-type header will not be set.
      return service.getData();
  }
  </code></pre>
  
  <p>The simply fix here is to simply add an if statement to check for null. But that's ugly as I'll have to manually set the header and the response body.</p>
  
  <p>I'm hoping someone knows of a nicer way?</p>
  
  <p>Thanks</p>