title: JAX-RS service always replies with 415 Unsupported Media Type error to POST requests
body: <p>I know (quite) similar questions was asked numerous times, but solutions to them didn't work for me.</p>
  
  <p>I'm creating a REST service with JAX-RS and Jersey 2 which consumes json beans from post requests, it looks like this:</p>
  
  <pre><code>@Path("auth")
  public class AuthService {
  @Inject
  private AuthenticationManager authenticationManager;
  
  @POST
  @Path("login")
  @Consumes(MediaType.APPLICATION_JSON)
  public Response login(ApiCredentials credentials){
      try {
          authenticationManager.login(credentials);
          return Response.ok().build();
      } catch (Exception e){
          e.printStackTrace();
          return Response.status(Response.Status.UNAUTHORIZED).build();
      }
  }
  </code></pre>
  
  <p>and runs in a programmatically configured embedded tomcat</p>
  
  <pre><code>public class WebServer {
  public static void main(String[] args) throws ServletException, LifecycleException, MalformedURLException{
      String webappDirLocation = "webapp/";
      Tomcat tomcat = new Tomcat();
      // Define port number for the web application
      String webPort = System.getenv("PORT");
      if (webPort == null || webPort.isEmpty()) {
          webPort = "8089";
      }
      // Bind the port to Tomcat server
      tomcat.setPort(Integer.valueOf(webPort));
  
      // Define a web application context.
      Context context = tomcat.addWebapp("/tomcatembedded", new File(
              webappDirLocation).getAbsolutePath());
  
      Tomcat.addServlet(context, "jersey-container-servlet", resourceConfig());
      context.addServletMapping("/rest/*", "jersey-container-servlet");
  
  
      tomcat.start();
      tomcat.getServer().await();
  }
  private static ServletContainer resourceConfig(){
      return new ServletContainer(
              new ResourceConfig(
                  new ResourceLoader().getClasses()
              )
              .register(new ApplicationBinder()) //binds AuthenticationManager
              .packages("com.dockerhosting.api.rest.auth", "com.dockerhosting.rest.auth")
      );
  }
  }
  </code></pre>
  
  <p>When I trying to post a json serialized ApiCredentials to the rest service it replies</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Apache Tomcat/8.0.22 - Error report&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 415 - Unsupported Media Type&lt;/h1&gt;&lt;div class="line"&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;type&lt;/b&gt; Status report&lt;/p&gt;&lt;p&gt;&lt;b&gt;message&lt;/b&gt; &lt;u&gt;Unsupported Media Type&lt;/u&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;description&lt;/b&gt; &lt;u&gt;The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.&lt;/u&gt;&lt;/p&gt;&lt;hr class="line"&gt;&lt;h3&gt;Apache Tomcat/8.0.22&lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>I tried to add mimepull to the classpath of the service and to add Content-Type header to the client, but it didn't change anything. The clinet code is:</p>
  
  <pre><code>JerseyClient client = new JerseyClientBuilder().build();
  
      JerseyWebTarget target = client.target("http://192.168.122.156:8089/tomcatembedded/rest/auth/login");
      Response response = target.request()
              .header("Content-Type", "application/json;charset=UTF-8")
              .accept(MediaType.APPLICATION_JSON)
              .post(Entity.entity(credentials, MediaType.APPLICATION_JSON));
      String output = response.readEntity(String.class);
      System.out.println(output);
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Thanks</p>