[EJB / REST / JSON / JQuery] Problème pour faire un POST
Bonjour à tous,
Je souhaite faire un petit chat web2.0 avec Ajax mais je n'arrive pas à poster des données au formats JSON.
Mes DTO sont très simples pour éviter de me prendre trop la tête au début:
ChatUser { int siteUserId, int anonId, String login }
ChatMessage { String content, CharUser sender}
Elles sont toutes annotées avec @XmlRootElement et @XmlElement/@XmlAttribute sur les getters.
Voici mon EJB de ressource REST:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
@Path("/chat")
@Stateless
@Produces("application/json")
@Consumes("application/json")
public class ChatResource {
@EJB
ChatsContainerLocal chatContainer;
public ChatResource() {
}
@GET
@Path("/get-lastmessages")
public List<ChatMessage> getLastMessages(@QueryParam("name") String name, @QueryParam("token") int token) {
System.out.println("REST SERVICE: chat/lastmsg@GET: " + name + " token=" + token);
List<ChatMessage> msgs = chatContainer.getLastMessages(name, token);
System.out.println("il y a " + msgs.size() + " nouveaux messages!");
return msgs;
}
@POST
@Path("/post-message")
public void addMessage(@QueryParam("chatName") String chatName, @QueryParam("chatMessage") ChatMessage chatMessage) {
System.out.println("REST SERVICE: addMessage to " + chatName + ": " + (chatMessage == null ? "null" : chatMessage.getContent()));
chatContainer.addMessage(chatName, chatMessage);
}
} |
Je formalise ma donnée JSON comme ceci:
Code:
1 2 3 4 5
|
var senderMessage = {anonId: 4, siteUserId: null, login: "anon"};
var chatMessage1 = {content: textAreaValue, sender: senderMessage};
var requestDatas = {chatName:"Chatoune",chatMessage: chatMessage1};
var datax = JSON.stringify(requestDatas); |
J'ai essayé ceci:
Code:
1 2 3 4 5 6 7 8
|
jQuery.post(
"http://localhost:8080/cocoon.services/resources/chat/post-message",
datax,
function (data, textStatus) {
alert("message posted! "+textStatus);
},
"json"); |
me retournant une erreur "415 Unsupported Media Type"
et cela:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| jQuery.ajax({
async : false,
type:"POST",
contentType: "application/json",
dataType: "application/json",
url: "http://localhost:8080/cocoon.services/resources/chat/post-message",
data: datax,
success: function(data) {
},
error: function(data) {
}
}); |
me retournant une erreur "400 Bad Request".
Pourtant les ressources GET passent niquel... Je ne comprend pas ce qui ne va pas...
Voici les traces de Jersey pour le jQUery.ajax():
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| X-Jersey-Trace-000 accept root resource classes: "/chat/post-message"
X-Jersey-Trace-001 match path "/chat/post-message" -> "/application\.wadl(/.)?", "/chat(/.)?"
X-Jersey-Trace-002 accept right hand path java.util.regex.Matcher[pattern=/chat(/.*)? region=0,18 lastmatch=/chat/post-message]: "/chat/post-message" -> "/chat" : "/post-message"
X-Jersey-Trace-003 accept resource: "chat" -> @Path("/chat") org.cianelli.cocoon.services.chat.rest.__EJB31_Generated__ChatResource__Intf____Bean__@129e956
X-Jersey-Trace-004 match path "/post-message" -> "/get-lastmessages(/)?", "/get-lastmessage(/)?", "/post-message(/)?", "/([^/]+?)/join/([^/]+?)(/)?", "/([^/]+?)/join(/)?", ""
X-Jersey-Trace-005 accept right hand path java.util.regex.Matcher[pattern=/post-message(/)? region=0,13 lastmatch=/post-message]: "/post-message" -> "/post-message" : ""
X-Jersey-Trace-006 accept sub-resource methods: "chat" : "/post-message", POST -> org.cianelli.cocoon.services.chat.rest.__EJB31_Generated__ChatResource__Intf____Bean__@129e956
X-Jersey-Trace-007 matched sub-resource method: @Path("/post-message") public void org.cianelli.cocoon.services.chat.rest.ChatResource.addMessage(java.lang.String,org.cianelli.cocoon.services.chat.ChatMessage)
X-Jersey-Trace-008 matched message body reader: class java.lang.String, "application/json; charset=UTF-8" -> com.sun.jersey.core.impl.provider.entity.StringProvider@3311c0
X-Jersey-Trace-009 matched message body reader: class org.cianelli.cocoon.services.chat.ChatMessage, "application/json; charset=UTF-8" -> com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App@1e06d94
X-Jersey-Trace-010 mapped exception to response: javax.ws.rs.WebApplicationException@12f5fb6 -> 400 (Bad Request)
X-Powered-By Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2 Java/Sun Microsystems Inc./1.6) |
J'ai l'impression que Jersey n'arrive pas à désérialiser le ChatUser dans le ChatMessage. Il se pourrait qu'il manque un paramètre quelque part...