I get an
Unsupported Media Type - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
Strangely, if I look at the POST calls in firebug, they are indicated as not successfull, but if I do a "resend", they are sent and an answer is retrieved.
I already tried the accepted answer there: jquery ajax rest call - Unsupported Media Type - did not work for me.
-edit: If it helps, I use Moxy.
var sent = "This is a test request.";
var add = "testing..";
var request = { sentence: sent, addition: add };
$.ajax({
url: "http://localhost:8080/MyProject/Rest/Path/toPost",
type: "POST",
data: JSON.stringify(request),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(resultData) {
//do stuff
},
});
This is my model:
@XmlRootElement(name = "request")
public class Request {
private String sentence;
private String addition;
public Request() {
this.sentence = "default";
this.addition = "default";
}
public Request(String sentence, String add) {
this.sentence = sentence;
this.addition = add;
}
public String getSentence() {
return sentence;
}
public String getAddition() {
return addition;
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public void setAddition(String addition) {
this.addition = addition;
}
}
@XmlRootElement(name = "answer")
public class Answer {
private ArrayList lsit;
private String info;
public Answer() {
this.list = new ArrayList();
this.info = "Good";
}
public Answer(ArrayList list, String info) {
this.list = list;
thisinfo = info;
}
public void setInfo(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public ArrayList getList() {
return list;
}
public void setList(ArrayList list) {
this.list = list;
}
}
And this is my Servlet:
@Path("/Path")
public class TestServlet {
@Path("/toPost")
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Answer consume(Request request) {
ArrayList res = new ArrayList();
res.add(request.getSentence());
return new Answer(res, "Good");
}
}