title: Request method 'POST' not supported in Spring Boot any suggestions?
body: <p>I'm building a web application using Spring Boot and Angular JS which will perform CRUD operations. I have successfully created one class and when implementing this class I'm getting this error "Request method 'POST' not supported"</p>
  
  <pre><code>    My controller class is:
  
          package com.fyp.controller;
  
          import com.fyp.masterdata.Truck;
          import com.fyp.masterdata.TruckRepository;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.http.MediaType;
          import org.springframework.web.bind.annotation.*;
  
          import java.util.ArrayList;
          import java.util.List;
  
          @CrossOrigin(origins = "http://localhost:4200")
          @RestController
          public class TruckController {
  
              @Autowired
              TruckRepository truckRepository;
  
  
              @GetMapping(value="/truck",  produces= MediaType.APPLICATION_JSON_VALUE)
              public List&lt;Truck&gt; getAll1() {
                  List&lt;Truck&gt; list = new ArrayList&lt;&gt;();
                  Iterable&lt;Truck&gt; truck = truckRepository.findAll();
                  truck.forEach(list::add);
                  return list;
              }
  
              @PostMapping(value="/posttruck")
              public Truck postTruck(@RequestBody Truck truck) {
                  truckRepository.save(new Truck(truck.getName(), truck.getCapacity()));
                  return truck;
              }
  
              @GetMapping(value="/getTruckByName/{name}",  produces= MediaType.APPLICATION_JSON_VALUE)
              public List&lt;Truck&gt; getTruckByName(@PathVariable String name) {
  
                  List&lt;Truck&gt; truck = truckRepository.getTruckByName(name);
                  return truck;
              }
  
              @DeleteMapping(value="/truck/{id}")
              public void deleteTruck(@PathVariable long id){
                  truckRepository.delete(id);
              }
          }
  </code></pre>
  
  <p>This is the error which I'm getting when I hit the above URL with POST request.</p>
  
  <pre><code>    &gt;#2018-05-11 18:42:46.868  WARN 10596 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound             : Request method 'POST' not supported
  </code></pre>
  
  <p>This is my front-end Angular JS code which is used to hit the URL. I'm running <a href="http://localhost:4200/truck" rel="nofollow noreferrer">http://localhost:4200/truck</a> and trying to get it work.</p>
  
  <pre><code>    // Get all trucks
        getTruck(): Promise&lt;Truck[]&gt; {
          return this.http.get(this.truckUrl)
            .toPromise()
            .then(response =&gt; response.json() as Truck[])
            .catch(this.handleError);
        }
        getTruckByName(name: string): Promise&lt;Truck[]&gt; {
          const url = `/findbytruck/${name}`;
          return this.http.get(url)
            .toPromise()
            .then(response =&gt; response.json() as Truck)
            .catch(this.handleError);
        }
  
        create1(truck: Truck): Promise&lt;Truck&gt; {
          return this.http
            .post("truck", JSON.stringify(truck), {headers : this.headers})
            .toPromise()
            .then(res =&gt; res.json() as Truck)
            .catch(this.handleError);
        }
  
        delete1(id: number): Promise&lt;void&gt; {
          const url = `${this.truckUrl}/${id}`;
          return this.http.delete(url, {headers: this.headers})
            .toPromise()
            .then(() =&gt; null)
            .catch(this.handleError);
        }
  
  Thank You.
  </code></pre>