Need to break down your requests. First, you want to accept a JSON string. So on your method you need
@Consumes(MediaType.APPLICATION_JSON)
Next, you need to decide what you want your method to obtain. You can obtain a JSON string, as you suggest, in which case your method would look like this:
@Consumes(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final String input) {
Or alternatively if your JSON string maps to a Java object you could take the object directly:
@Consumes(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final MyObject input) {
You state that you want to return a JSON string. So you need:
@Produces(MediaType.APPLICATION_JSON)
And then you need to actually return a JSON string:
return "{\"result\": \"Hello world\"}";
So your full method looks something like this:
@PATH("/hello")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final String input) {
return "{\"result\": \"Hello world\"}";
}
Regarding using AJAX to send and receive, it would look something like this:
var myData="{\"name\": \"John\"}";
var request = $.ajax({
url: "/hello",
type: "post",
data: myData
});
request.done(function (response, textStatus, jqXHR){
console.log("Response from server: " + response);
});