0

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:

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);
    }

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

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

This is the data the server expects:

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

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..

The Eleve class

    @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 && other.idEleve != null) || (this.idEleve != null && !this.idEleve.equals(other.idEleve))) {
            return false;
        }
        return true;
    }

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

     }

NOTE: Also worth to mention that when i replace

conn.setRequestProperty("contentType", "application/json");

by

conn.setRequestProperty("Content-Type", "application/json");

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

Ilias
  • 86
  • 1
  • 13
  • 415 error means `Unsupported Media Type` so your post has wrong data or the method you are trying to call doesn't expect what you are sending. Can you show the Eleve class? – Jorge Campos May 25 '16 at 04:12
  • I added the Eleve class but i doubt there is something wrong with it, seeing how it successfully inserts when i try the same JSONObject in glassfish test mode. – Ilias May 25 '16 at 10:03

0 Answers0