title: How to respond with an HTTP 400 error in a Spring MVC @ResponseBody method returning String
body: <p>I'm using Spring MVC for a simple JSON API, with <code>@ResponseBody</code> based approach like the following. (I already have a service layer producing JSON directly.)</p>
  <pre><code>@RequestMapping(value = &quot;/matches/{matchId}&quot;, produces = &quot;application/json&quot;)
  @ResponseBody
  public String match(@PathVariable String matchId) {
      String json = matchService.getMatchJson(matchId);
      if (json == null) {
          // TODO: how to respond with e.g. 400 &quot;bad request&quot;?
      }
      return json;
  }
  </code></pre>
  <p>In the given scenario, what is the simplest, cleanest way to respond with a HTTP 400 error?</p>
  <p>I did come across approaches like:</p>
  <pre><code>return new ResponseEntity(HttpStatus.BAD_REQUEST);
  </code></pre>
  <p>...but I can't use it here since my method's return type is String, not ResponseEntity.</p>