title: @ControllerAdvice not catching Rest API exceptions
body: <p>I'm trying to implement Error handling into my Rest API and testing with Postman, when I give an incorrect path postman returns a 404 but its a 404 HTML. I am using the @ControllerAdvice for the global exception handler. </p>
  
  <p>the @ControllerAdvice class is RestResponseEntityExceptionHandler.java</p>
  
  <pre><code>@ControllerAdvice
  public class RestResponseEntityExceptionHandler  
    extends ResponseEntityExceptionHandler {
  
      @ExceptionHandler(value  = { IllegalArgumentException.class, IllegalStateException.class })
      protected ResponseEntity&lt;Object&gt; handleConflict(
        RuntimeException ex, WebRequest request) {
          String bodyOfResponse = "This should be application specific";
          return handleExceptionInternal(ex, bodyOfResponse, 
            new HttpHeaders(), HttpStatus.CONFLICT, request);
      }
  }
  </code></pre>
  
  <p>I was told that by default spring boot should return something like this</p>
  
  <pre><code>{
    "timestamp": 1436442596410,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/item"
  }
  </code></pre>
  
  <p>the nonexistent path I give it is</p>
  
  <pre><code>http://localhost:8080/Assignment2C/breweriessdfsdf
  </code></pre>
  
  <p>the response I get is</p>
  
  <pre><code>    &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
      &lt;title&gt;HTTP Status 404 – Not Found&lt;/title&gt;
      &lt;style type="text/css"&gt;
          h1 {
              font-family: Tahoma, Arial, sans-serif;
              color: white;
              background-color: #525D76;
              font-size: 22px;
          }
  
          h2 {
          font-family: Tahoma, Arial, sans-serif;
          color: white;
          background-color: #525D76;
          font-size: 16px;
      }
  
      h3 {
          font-family: Tahoma, Arial, sans-serif;
          color: white;
          background-color: #525D76;
          font-size: 14px;
      }
  
      body {
          font-family: Tahoma, Arial, sans-serif;
          color: black;
          background-color: white;
      }
  
      b {
          font-family: Tahoma, Arial, sans-serif;
          color: white;
          background-color: #525D76;
      }
  
      p {
          font-family: Tahoma, Arial, sans-serif;
          background: white;
          color: black;
          font-size: 12px;
      }
  
      a {
          color: black;
      }
  
      a.name {
          color: black;
      }
  
      .line {
          height: 1px;
          background-color: #525D76;
          border: none;
      }
  &lt;/style&gt;
  &lt;/head&gt;
  
      &lt;body&gt;
      &lt;h1&gt;HTTP Status 404 – Not Found&lt;/h1&gt;
      &lt;hr class="line" /&gt;
      &lt;p&gt;&lt;b&gt;Type&lt;/b&gt; Status Report&lt;/p&gt;
      &lt;p&gt;&lt;b&gt;Description&lt;/b&gt; The origin server did not find a current representation for the target resource or is not
          willing to disclose that one exists.&lt;/p&gt;
      &lt;hr class="line" /&gt;
      &lt;h3&gt;Apache Tomcat/9.0.26&lt;/h3&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>the spring boot dependency that I am using is</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
      &lt;version&gt;2.2.6.RELEASE&lt;/version&gt;
      &lt;type&gt;pom&lt;/type&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>my controller class is Breweries_Controller</p>
  
  <pre><code>  @RestController
  @RequestMapping("/breweries")
  public class Breweries_Controller {
  
      @Autowired
      Breweries_Service service;
  
      @GetMapping(produces = MediaTypes.HAL_JSON_VALUE)
      public Resources&lt;Breweries&gt; getAllBreweries(@RequestParam(name = "limit", required = false) Integer limit , @RequestParam(name = "offset", required = false) Integer offset) {
  
          List&lt;Breweries&gt; allBreweries = service.getAllBreweries();
  
          if(limit == null &amp;&amp; offset == null){
              limit = 20;
              offset = 0;
          }
          List&lt;Breweries&gt; paginatedList = allBreweries.subList(offset, offset + limit);
  
          for (Breweries b : allBreweries) {
              int id = b.getResourceId();
              Link self = linkTo(this.getClass()).slash(id).withSelfRel();
              b.add(self);
              linkTo(methodOn(this.getClass()).getBrewerie(id));
          }
          Link link = linkTo(this.getClass()).withSelfRel();
          Resources&lt;Breweries&gt; result = new Resources&lt;Breweries&gt;(paginatedList, link);
  
          return result;
  
      }
  
      @GetMapping(value = "/{id}", produces = MediaTypes.HAL_JSON_VALUE)
      public Resource&lt;Breweries&gt; getBrewerie(@PathVariable("id") int id) {
          Resource&lt;Breweries&gt; brewerie = new Resource&lt;Breweries&gt;(service.getBrewerieById(id));
          ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).getAllBreweries(5, 50));
          brewerie.add(linkTo.withRel("all-breweries"));
          return brewerie;
      }
  
      @DeleteMapping(value = "/{id}")
      @ResponseStatus(HttpStatus.OK)
      public void delete(@PathVariable("id") int id) {
          Breweries brewerie = service.getBrewerieById(id);
          service.deleteBrewerie(brewerie);
      }
  
      @PostMapping
      @ResponseStatus(HttpStatus.CREATED)
      public void create(@RequestBody Breweries b) {
          b.setResourceId(0);
          b.setAddUser(0);
          b.setLastMod(new Date());
          service.addBrewerie(b);
      }
  
      @PutMapping(value = "/{id}")
      @ResponseStatus(HttpStatus.OK)
      public void update(@PathVariable("id") int id, @RequestBody Breweries b) {
          b.setResourceId(id);
          b.setAddUser(0);
          b.setLastMod(new Date());
          service.editBrewerie(b);
      }
  }
  </code></pre>