title: error 405 in rest api with method post
body: <p>I have a problem about the method post in my program java rest api. I use as back-end: jersey and hibernate and as front-end: angular. when I consume web service by method post in angular application and test program in web browser, the console browser show an error as: </p>
  
  <blockquote>
    <p>"Failed to load resource: the server responded with a status of 405 (Non Allowed Method)"</p>
  </blockquote>
  
  <p>I see a tutorial about this problem and it give me a solution that use a CORS which manages the http method and I understand about the CORS but my problems are:
  -how to code this CORS in my java program?
  -should I create a class with CORS method?
  -where should I call CORS in my program?
  Please help me. thank you very much</p>
  
  <p>it's rest api</p>
  
  <pre><code>package com.ws.rest;
  
  import java.util.List;
  
  import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  import com.ws.dao.DaoFactory;
  import com.ws.entity.Personne;
  import com.ws.jpa.PersonneJPA;
  
  @Path("/personne")
  public class PersonneRest {
  
      private PersonneJPA jpa_personne;
  
      public PersonneRest() {
          this.jpa_personne = DaoFactory.getInstance().get_personne();
      }
  
      /*
       * #########################################################################
       * PROCEDURES
       * #########################################################################
       */
  
      // Getall
      @Path("/getall")
      @GET
      @Produces(MediaType.APPLICATION_JSON)
      public List&lt;Personne&gt; getAll() {
          return jpa_personne.findAll();
      }
  
      // Get par ID
      @Path("/{id}")
      @GET
      @Produces(MediaType.APPLICATION_JSON)
      public Personne getById(@PathParam("id") long id) {
          List&lt;Personne&gt; h_personne = jpa_personne.findById(id);
          if (h_personne == null)
              return null;
          return h_personne.get(0);
      }
  
      // Ajout d'un enregistrement de type Langue
      @Path("/add")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void add(Personne personne) {
          jpa_personne.add(personne);
      }
  
      // Mise a jour d'un enregistrement de type Langue
      @Path("/update")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void update(Personne personne) {
          jpa_personne.update(personne);
      }
  
      // Suppression d'un enregistrement de type Langue
      @Path("/delete")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void delete(Personne personne) {
          jpa_personne.delete(personne);
      }
  }
  </code></pre>
  
  <p>Here is the web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"&gt;
    &lt;servlet&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;!-- Define the ResourceConfig class --&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
              &lt;param-value&gt;com.ws.rest&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;!-- Map all the URLs to the Jersey ServletContainer --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>