如何从 Restful 网络服务中的 Ajax 调用中捕获数据参数?
How to capture data parameter from Ajax call in Restful webservice?
我有一个 ajax 调用,我正在调用一个 Restful WS 传递一个字符串 -
var b2bValues = "Some String";
var lookupUrl = "http://ip:{port}/ImageViewer/rest/ImageServlet/callrest";
var lookupReq = $.ajax({url:lookupUrl, async:false, data:b2bValues, contentType: "application/xml", type: "POST"});
我的Restful代码是-
@POST
@Path("/callrest")
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces({"application/xml", "application/json", "text/plain"})
public ImageFieldsList getImageFromSource(@QueryParam("b2bValues") String b2b)
{//Some code
}
服务器端的 b2bValues 为空。
我的问题是如何更改 Restful 代码以捕获从 Ajax 调用传递的数据参数??
有两种方法可以解决此问题
@QueryParam
将查找请求中可用的参数
URL。所以,如果你需要在服务器端使用 @QueryParam
,你会
需要修改URL,发送数据如下:
var b2bValues = "Some String";
var lookupUrl = "http://ip:{port}/ImageViewer/rest/ImageServlet/callrest?b2bValues="+b2bValues;
var lookupReq = $.ajax({url:lookupUrl, async:false, contentType: "application/xml", type: "POST"});
在这种情况下不需要在服务器端进行任何更改。
一般情况下,对于POST
个请求,我们通过forming request发送数据
对象。所以,在客户端,你会像这样形成请求对象
这个:
var requestData = { b2bValues : "SomeValue",
someOtherParam : "SomeOtherParamValue",
anotherParam : "AnotherParamValue"
}
var lookupReq = $.ajax({url:lookupUrl, async:false, data:JSON.stringify(requestData), contentType: "application/xml", type: "POST"});
并且在服务器端,您需要具有等效的值对象
保存请求数据。成员变量的名称应该匹配
您在请求中发送的那些(反之亦然)
示例请求对象
// SampleRequestObject.java
String b2bValues;
String someOtherParam;
String anotherParam;
// getters and setters for these member variables
现在,ReST 方法将更改为:
@POST
@Path("/callrest")
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces({"application/xml", "application/json", "text/plain"})
public ImageFieldsList getImageFromSource(SampleRequestObject requestInput) {
// access request input using getters of SampleRequestObject.java
// For example, requestInput.getB2bValues();
}
它需要更改服务器代码 -
@POST
@Path("/callrest")
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces({"application/xml", "application/json", "text/plain"})
public ImageFieldsList getImageFromSource(String b2bValues)
{//Some code
}
不需要使用@Queryparam,但key要和变量名匹配。
我有一个 ajax 调用,我正在调用一个 Restful WS 传递一个字符串 -
var b2bValues = "Some String";
var lookupUrl = "http://ip:{port}/ImageViewer/rest/ImageServlet/callrest";
var lookupReq = $.ajax({url:lookupUrl, async:false, data:b2bValues, contentType: "application/xml", type: "POST"});
我的Restful代码是-
@POST
@Path("/callrest")
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces({"application/xml", "application/json", "text/plain"})
public ImageFieldsList getImageFromSource(@QueryParam("b2bValues") String b2b)
{//Some code
}
服务器端的 b2bValues 为空。
我的问题是如何更改 Restful 代码以捕获从 Ajax 调用传递的数据参数??
有两种方法可以解决此问题
@QueryParam
将查找请求中可用的参数 URL。所以,如果你需要在服务器端使用@QueryParam
,你会 需要修改URL,发送数据如下:var b2bValues = "Some String"; var lookupUrl = "http://ip:{port}/ImageViewer/rest/ImageServlet/callrest?b2bValues="+b2bValues; var lookupReq = $.ajax({url:lookupUrl, async:false, contentType: "application/xml", type: "POST"});
在这种情况下不需要在服务器端进行任何更改。
一般情况下,对于
POST
个请求,我们通过forming request发送数据 对象。所以,在客户端,你会像这样形成请求对象 这个:var requestData = { b2bValues : "SomeValue", someOtherParam : "SomeOtherParamValue", anotherParam : "AnotherParamValue" } var lookupReq = $.ajax({url:lookupUrl, async:false, data:JSON.stringify(requestData), contentType: "application/xml", type: "POST"});
并且在服务器端,您需要具有等效的值对象 保存请求数据。成员变量的名称应该匹配 您在请求中发送的那些(反之亦然)
示例请求对象
// SampleRequestObject.java String b2bValues; String someOtherParam; String anotherParam; // getters and setters for these member variables
现在,ReST 方法将更改为:
@POST @Path("/callrest") @Consumes({"application/xml", "application/json", "text/plain"}) @Produces({"application/xml", "application/json", "text/plain"}) public ImageFieldsList getImageFromSource(SampleRequestObject requestInput) { // access request input using getters of SampleRequestObject.java // For example, requestInput.getB2bValues(); }
它需要更改服务器代码 -
@POST
@Path("/callrest")
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces({"application/xml", "application/json", "text/plain"})
public ImageFieldsList getImageFromSource(String b2bValues)
{//Some code
}
不需要使用@Queryparam,但key要和变量名匹配。