I want to create a REST Jersey Web-Service accepting JSON string as input parameter.
我想创建一个REST Jersey Web服务,接受JSON字符串作为输入参数。
Also I will use post requestand from webmethod I will return one JSON string.
此外,我将使用post request和webmethod我将返回一个JSON字符串。
How can I consume this in a HTML page using Ajax post request. I want to know what all changes I need to make it on web method to accept JSON String.
如何使用Ajax post请求在HTML页面中使用它。我想知道我需要在web方法上接受JSON字符串的所有更改。
public class Hello {
@POST
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
2 个解决方案
#1
26
Need to break down your requests. First, you want to accept a JSON string. So on your method you need
需要打破您的要求。首先,您想要接受JSON字符串。所以你需要的方法
@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:
接下来,您需要确定您希望方法获得的内容。您可以按照建议获取JSON字符串,在这种情况下,您的方法如下所示:
@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:
或者,如果您的JSON字符串映射到Java对象,您可以直接获取该对象:
@Consumes(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final MyObject input) {
You state that you want to return a JSON string. So you need:
您声明要返回JSON字符串。所以你需要:
@Produces(MediaType.APPLICATION_JSON)
And then you need to actually return a JSON string:
然后你需要实际返回一个JSON字符串:
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:
关于使用AJAX发送和接收,它看起来像这样:
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);
});
#2
0
This will work. "path" is the relative URL path to be used in AJAX call.
这会奏效。 “path”是在AJAX调用中使用的相对URL路径。
public class Hello {
@POST
@Path("/path")
@Produces({ "text/html" })
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
}
#1
26
Need to break down your requests. First, you want to accept a JSON string. So on your method you need
需要打破您的要求。首先,您想要接受JSON字符串。所以你需要的方法
@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:
接下来,您需要确定您希望方法获得的内容。您可以按照建议获取JSON字符串,在这种情况下,您的方法如下所示:
@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:
或者,如果您的JSON字符串映射到Java对象,您可以直接获取该对象:
@Consumes(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final MyObject input) {
You state that you want to return a JSON string. So you need:
您声明要返回JSON字符串。所以你需要:
@Produces(MediaType.APPLICATION_JSON)
And then you need to actually return a JSON string:
然后你需要实际返回一个JSON字符串:
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:
关于使用AJAX发送和接收,它看起来像这样:
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);
});
#2
0
This will work. "path" is the relative URL path to be used in AJAX call.
这会奏效。 “path”是在AJAX调用中使用的相对URL路径。
public class Hello {
@POST
@Path("/path")
@Produces({ "text/html" })
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
}