title: How to enable CORS in Spring Boot - Not working
body: <p>I am trying to enable CORS in my Spring Boot app, but it is not working at all.</p>
  
  <hr>
  
  <p><strong>I have tried</strong></p>
  
  <ul>
  <li><code>@CrossOrigin</code> annotation</li>
  <li><a href="https://stackoverflow.com/questions/32319396/cors-with-spring-boot-and-angularjs-not-working">CORS with spring-boot and angularjs not working</a></li>
  <li>And this <a href="https://spring.io/guides/gs/rest-service-cors/" rel="noreferrer">https://spring.io/guides/gs/rest-service-cors/</a></li>
  </ul>
  
  <p>I have no clue how to fix this at this point in time, nor what the issue is with CORS not working.</p>
  
  <hr>
  
  <p>My code</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RestController
  public class DbController {
  
      @Autowired
      private IDAO conn;
  
      @CrossOrigin
      @GetMapping("/foo")
      public List&lt;Foo&gt; getFoo() {
          return conn.getFooFromDao();
      }
  }
  </code></pre>
  
  <p><strong>DAO</strong></p>
  
  <pre><code>    @Repository
      public class DaoImpl implements IDAO {
  
      @Autowired
      private JdbcTemplate temp;
  
      public List&lt;Foo&gt; getFooFromDao() {
  
          List&lt;Foo&gt; data = new ArrayList&lt;&gt;();
  
          String sql = "SELECT fooName FROM BigFoo ORDER BY fooName ASC;";
  
          data.addAll(temp.query(sql, new BeanPropertyRowMapper(BigFoo.class)));
          return data;
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Expected</strong>:</p>
  
  <p>I want my controller to be accessible from <em>any</em> origin/domain with <em>any</em> method.</p>
  
  <p><strong>Actual</strong>:</p>
  
  <p>My controller is <em>not</em> accessible from <em>any</em> origin/domain. It gives my Angular frontend an error:</p>
  
  <p><strong>EDIT: My error in the frontend</strong></p>
  
  <blockquote>
    <p>Access to XMLHttpRequest at 'localhost:8080/foo' from origin
    '<a href="http://localhost:4200" rel="noreferrer">http://localhost:4200</a>' has been blocked by CORS policy: Cross origin
    requests are only supported for protocol schemes: http, data, chrome,
    chrome-extension, https.</p>
  </blockquote>
  
  <p><strong>Second edit</strong>:</p>
  
  <p>This is my code in Angular (frontend): </p>
  
  <p><strong>Service</strong></p>
  
  <pre><code>getFoo() {
      return this.http.get("localhost:8080/foo");
    }
  </code></pre>
  
  <p>I am using <code>HttpClient</code> from <code>import { HttpClient } from "@angular/common/http";</code></p>
  
  <p>I also verified that the URL works in that service method by copy &amp; pasting it into my browser. It does indeed, return JSON, which omits the possibility of a wrong URL or a typo.</p>