HTTP response code: 415 when sending JSON data to REST using java
<p>so i'm using a JAVA application to try to send a POST to a RESTful server (also in JAVA) using json, however i keep getting the 415 HTTP response, this is my client code:</p>

<pre><code>URL url;
    try {
        url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("contentType", "application/json");
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(data);
        System.out.println(data);
        } catch (Exception e) {
        System.out.println(e);
    }
</code></pre>

<p>And this is the exception i get along with the data i'm trying to send (i omitted the link):</p>

<pre><code>{"idEleve":"null","pseudo":"ddd","pass":"ddd"}
java.io.IOException: Server returned HTTP response code: 415 for URL: ...
</code></pre>

<p>This is the data the server expects:</p>

<pre><code>@POST
@Override
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public void create(Eleve entity) {
    super.create(entity);
}
</code></pre>

<p>So obviously it is already expecting a JSON and there is nothing wrong with the service itself as when i try to test it using the exact jsonObject im trying to send, it creates it successfully..</p>

<p>The Eleve class</p>

<pre><code>    @Entity
@Table(name = "eleve")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Eleve.findAll", query = "SELECT e FROM Eleve e"),
    @NamedQuery(name = "Eleve.findByIdEleve", query = "SELECT e FROM Eleve e WHERE e.idEleve = :idEleve")})
public class Eleve implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_eleve")
    private Integer idEleve;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "pseudo")
    private String pseudo;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "pass")
    private String pass;

    public Eleve() {
    }

    public Eleve(Integer idEleve) {
        this.idEleve = idEleve;
    }

    public Eleve(Integer idEleve, String pseudo, String pass) {
        this.idEleve = idEleve;
        this.pseudo = pseudo;
        this.pass = pass;
    }

    public Integer getIdEleve() {
        return idEleve;
    }

    public void setIdEleve(Integer idEleve) {
        this.idEleve = idEleve;
    }

    public String getPseudo() {
        return pseudo;
    }

    public void setPseudo(String pseudo) {
        this.pseudo = pseudo;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idEleve != null ? idEleve.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Eleve)) {
            return false;
        }
        Eleve other = (Eleve) object;
        if ((this.idEleve == null &amp;&amp; other.idEleve != null) || (this.idEleve != null &amp;&amp; !this.idEleve.equals(other.idEleve))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "model.Eleve[ idEleve=" + idEleve + " ]";
    }

     }
</code></pre>

<p>NOTE: Also worth to mention that when i replace</p>

<pre><code>conn.setRequestProperty("contentType", "application/json");
</code></pre>

<p>by </p>

<pre><code>conn.setRequestProperty("Content-Type", "application/json");
</code></pre>

<p>i get a 400 HTTP error, and unlike the other threads my glassfish tests using the same JSON data works.</p>