Hi,
I would like to write a restful service which use @GET and
@Consumes(MediaType.APPLICATION_JSON). I can only google example for @POST.
I don't know whether I need to use @QueryParam for my parameters for GET
method. If yes, how does the request line of the HTTP request become?
Thanks.
Franz
(In real environment, the request line is url encodeded)
1. GET
/myservice?myRequest={"actionList":["action1","action2"],"triggerName":"triggerName1"}
HTTP/1.1
2. GET
/myservice?{"myRequest":{"actionList":["action1","action2"],"triggerName":"triggerName1"}}
HTTP/1.1
Here is my code.
@Singleton
@Path("/")
public class MyService {
@Path("/myservice")
// Use GET because jQuery.ajax sends OPTIONS instead of POST when using
POST
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MyResponse action(MyRequest myRequest) {
return new MyResponse(); // currently no logic
}
}
@XmlRootElement
public class MyRequest {
private List<String> actionList = new ArrayList<String>();
private String triggerName;
... getters and setters
}
@XmlRootElement
public class MyResponse {
private String result;
... getters and setters
}
For reference, here is my client (in javascript) calling the service.
$.ajax({
type: "GET",
url: "
http://localhost:8080/myservice",
dataType: "json",
data: "{" +
"\"myRequest\" : {" +
"\"actionList\" : [\"action1\",\"action2\"]," +
"\"triggerName\" : \"triggerName1\"" +
"}" +
"}",
contentType: "application/json; charset=UTF-8",
success: function(msg){
alert("ok");
},
error: function(xhr, msg) { alert(msg + '\n' + xhr.responseText); }
});