title: AJAX Post request to remote tomcat returns 415
body: <p>My problem is that each POST requests sent with AJAX return a 415 error while using a remote tomcat hosted on a Debian 9 VM. The same code works fine for a local tomcat (same version, no difference in the config files).
  Tested with different versions of tomcat (8.5 and 9). Same problem. </p>
  
  <p>The considered Login page (see below) :</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;div id="form"&gt;
      &lt;form id="loginEmp-Form"&gt;
          &lt;input type="email" id="login" name="login" placeholder="email" /&gt;
          &lt;input type="password" id="password" name="password" placeholder="password" /&gt; 
          &lt;input type="submit" id="loginButton" value="Connection" /&gt;
      &lt;/form&gt;
      &lt;div id="errorMsg"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>JAVASCRIPT</strong></p>
  
  <pre><code>$('#loginEmp-Form').submit(function(event) {
      event.preventDefault();
      var loginReceived = loginForm.login.value;
      var passwordReceived = loginForm.password.value;
      //I deliberately deleted the logic concerning the password hashing to make it simple
  
      var Login = {
          login: loginReceived,
          password: passwordReceived
      };
      $.ajax({
          headers : {'Content-Type' : 'application/json;charset=utf-8'},
          type : 'POST',
          url : 'login',
          data : JSON.stringify(Login),
          success : function(result) {
              // DO SOMETHING
          },
          error : function(e) {
              // DO SOMETHING
          }
      });
  }
  </code></pre>
  
  <p><strong>JAVA</strong></p>
  
  <p><em>Controller</em></p>
  
  <pre><code>public class LoginController {
  
        private static final Logger LOGGER=LoggerFactory.getLogger(LoginController.class);
  
        @RequestMapping(value = "/login", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
        public ModelAndView processLoginEmployee(@RequestBody Login loginReceived, HttpSession session) {
              // Logic not detailed here because we're not even entering in the controller. Checked with the following traces.
              LOGGER.info("Received request POST"); // nothing display
              ModelAndView mav = null;
              return mav;
        }
  }
  </code></pre>
  
  <p><em>Corresponding Bean</em></p>
  
  <pre><code>public class Login {
  
       private String login;
       private String password;
  
       /*
       * Constructors
       */
  
       public Login(String login, String password) {
            super();
            this.login = login;
            this.password= password;
       }
  
       public Login() {
       }
  
       /*
       * Getters &amp; setters
       */
  
       public String getLogin() {
            return login;
       }
  
       public void setLogin(String login) {
            this.login = login;
       }
  
       public String getPassword() {
            return password;
       }
  
       public void setPassword(String password) {
            this.password= password;
  
       }
  }
  </code></pre>
  
  <p>As I said, when I'm deploying the application on a local Tomcat, everything works fine (front-end and back-end). I'm understanding that a 415 error refers to a problem of request format. But I can't really tell where is it coming from. So that's the reason why I'm asking for help! I've spent hours trying to search for the solution on the internet, but without success.</p>
  
  <p>Other information: </p>
  
  <ul>
  <li>GET Requests work fine, even while using remote Tomcat.</li>
  <li>I'm working on my company internet network. Could any security element block post request ? Like a firewall ? I claim that no, because some colleagues developped and deployed their apps on remote tomcat and POST requests just worked perfectly. I asked for their help but they didn't find any solution. </li>
  </ul>
  
  <p>What I've already tried --> <em>Ajax call</em> :</p>
  
  <ul>
  <li>add/delete headers with 'content-type: application/json'</li>
  <li>add/delete 'ContentType: application/json'</li>
  </ul>
  
  <p><em>Java Controller</em> :</p>
  
  <ul>
  <li>add/delete 'consumes=MediaType.APPLICATION_JSON_VALUE'</li>
  </ul>
  
  <p><em>Config of Tomcat</em> :</p>
  
  <ul>
  <li>checking if the size limit of requests is fine</li>
  <li>checking if tomcat is opened to remote connection</li>
  </ul>
  
  <p>I've tried with Postman to send the exact same request. Result: 415.</p>
  
  <p>I'll appreciate a lot if someone finds the solution or at least has proposals. Thank you in advance for your time.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Problem solved. 2 things :</p>
  
  <p>First, in the pom.xl, only use this dependency and not the older one from org.codehaus. </p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;${jackson.version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Maven is going to create automatically each needed jars while building. Also, another mistake was to use both dependencies simultaneously (from codehaus and fasterxml). The dependencies from fasterxml are the ones to be used now. </p>
  
  <p>Then, modifications have been done in the controller: </p>
  
  <pre><code>@RequestMapping(value = "/login", method = RequestMethod.POST)
  public String processLoginEmployee(@RequestBody String json)
          throws JsonParseException, JsonMappingException, IOException {
      LOGGER.info("Received request POST");
      ObjectMapper mapper = new ObjectMapper();
      Login employee = mapper.readValue(json, Login.class);
      System.out.println(employee.getLogin());
      System.out.println(employee.getMdp());
      return "success";
  }
  </code></pre>
  
  <p>Attempting to get an object directly with @RequestParam doesn't work. 
  Fetching a json as a string and then parsing it with an ObjectMapper gets the job done. </p>