RESTful - 在 Queryparam 中获取空值
RESTful - Getting null in Queryparam
获取空响应。
这是我的代码
package com.javacodegeeks.enterprise.rest.jersey;
import java.util.Date;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
@Path("/")
public class HelloWorldREST {
@POST
@Path("/submitValue")
public Response responseMsg(@QueryParam("name") String name,@QueryParam("email") String email,@QueryParam("date") Date date) {
String output = date+email+name;
System.out.println(output);
return Response.status(200).entity(output).build();
}
}
这是对 URL
的调用
Ext.Ajax.request({
method : 'post',
url: 'rest/submitValue/',
//success: someFn,
//failure: otherFn,
params:
{
name: Ext.getCmp('name').getValue(),
email : Ext.getCmp('email').getValue(),
date : Ext.getCmp('date').getValue()
}
});
我检查过当我发送这个请求参数时是否已通过...所以它们的值不可能为空。
您可以使用 data: 而不是 params:
将数据发送到服务器
Ext.Ajax.request({
method : 'post',
url: 'rest/submitValue/',
//success: someFn,
//failure: otherFn,
data:
{
name: Ext.getCmp('name').getValue(),
email : Ext.getCmp('email').getValue(),
date : Ext.getCmp('date').getValue()
}
});
我在这里看到了三件事
- 您使用的 jquery 是哪个版本??如果它 < 1.9.0 那么你应该使用
type:
而不是 method:
类型定义:来自jquery网站:
type (default: 'GET')
Type: String
An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.
您的控制器 (HelloWorldREST) 上没有 @Component
或 @Controller
注释。
从 Rest 的角度来看,对于 POST 调用,您不应该使用查询参数而不是将数据作为负载发送。并在您的控制器上添加 @Consumes(MediaType.APPLICATION_JSON)
。
这可能会有所帮助...
首先你的休息方法即 HelloWorldREST.responseMsg 不应该是 post 因为它没有 requestBody。应该是get方法。永远记住,如果您想将数据作为查询参数(url 参数)发送,请始终使用 get,如果您想在请求正文中发送数据,请使用 POST.
现在是第二件事,因为您希望将数据作为查询参数发送,您可以通过两种方式进行。确保您从查询中获取值 Ext.getCmp('name').getValue() ,您可以在 chrome 开发人员控制台中检查它们的值。
Ext.Ajax.request({
url: 'rest/submitValue?name=' + Ext.getCmp('name').getValue() + '&email=' + Ext.getCmp('email').getValue() + '&date=' + Ext.getCmp('date').getValue(),
method: 'GET',
success: function (response) {
},
failure:function(response){
}
});
Ext.Ajax.request({
url: 'rest/submitValue?name=' + Ext.getCmp('name').getValue() + '&email=' + Ext.getCmp('email').getValue() + '&date=' + Ext.getCmp('date').getValue(),
method: 'GET',
params: {
name: Ext.getCmp('name').getValue(),
email : Ext.getCmp('email').getValue(),
date : Ext.getCmp('date').getValue()
}
success: function (response) {
},
failure:function(response){
}
});
获取空响应。
这是我的代码
package com.javacodegeeks.enterprise.rest.jersey;
import java.util.Date;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
@Path("/")
public class HelloWorldREST {
@POST
@Path("/submitValue")
public Response responseMsg(@QueryParam("name") String name,@QueryParam("email") String email,@QueryParam("date") Date date) {
String output = date+email+name;
System.out.println(output);
return Response.status(200).entity(output).build();
}
}
这是对 URL
的调用Ext.Ajax.request({
method : 'post',
url: 'rest/submitValue/',
//success: someFn,
//failure: otherFn,
params:
{
name: Ext.getCmp('name').getValue(),
email : Ext.getCmp('email').getValue(),
date : Ext.getCmp('date').getValue()
}
});
我检查过当我发送这个请求参数时是否已通过...所以它们的值不可能为空。
您可以使用 data: 而不是 params:
将数据发送到服务器Ext.Ajax.request({
method : 'post',
url: 'rest/submitValue/',
//success: someFn,
//failure: otherFn,
data:
{
name: Ext.getCmp('name').getValue(),
email : Ext.getCmp('email').getValue(),
date : Ext.getCmp('date').getValue()
}
});
我在这里看到了三件事
- 您使用的 jquery 是哪个版本??如果它 < 1.9.0 那么你应该使用
type:
而不是method:
类型定义:来自jquery网站:
type (default: 'GET')
Type: String
An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.
您的控制器 (HelloWorldREST) 上没有
@Component
或@Controller
注释。从 Rest 的角度来看,对于 POST 调用,您不应该使用查询参数而不是将数据作为负载发送。并在您的控制器上添加
@Consumes(MediaType.APPLICATION_JSON)
。
这可能会有所帮助...
首先你的休息方法即 HelloWorldREST.responseMsg 不应该是 post 因为它没有 requestBody。应该是get方法。永远记住,如果您想将数据作为查询参数(url 参数)发送,请始终使用 get,如果您想在请求正文中发送数据,请使用 POST.
现在是第二件事,因为您希望将数据作为查询参数发送,您可以通过两种方式进行。确保您从查询中获取值 Ext.getCmp('name').getValue() ,您可以在 chrome 开发人员控制台中检查它们的值。
Ext.Ajax.request({
url: 'rest/submitValue?name=' + Ext.getCmp('name').getValue() + '&email=' + Ext.getCmp('email').getValue() + '&date=' + Ext.getCmp('date').getValue(),
method: 'GET',
success: function (response) {
},
failure:function(response){
}
});
Ext.Ajax.request({
url: 'rest/submitValue?name=' + Ext.getCmp('name').getValue() + '&email=' + Ext.getCmp('email').getValue() + '&date=' + Ext.getCmp('date').getValue(),
method: 'GET',
params: {
name: Ext.getCmp('name').getValue(),
email : Ext.getCmp('email').getValue(),
date : Ext.getCmp('date').getValue()
}
success: function (response) {
},
failure:function(response){
}
});