【发布时间】:2016-08-23 11:14:36
【问题描述】:
这是我第一次为我的 Web 应用程序使用 Java 后端。我有一个 Jax-rs 网络服务,我试图通过我的 AngularJS 应用程序使用它
AngularJS 调用
$http({
url : REST_END_POINT + '/checkuser',
method : "GET",
data : {
'userId' : credentials.username,
'pwd' : credentials.password
},
dataType : "json",
headers : {
"Content-Type" : "application/json"
}
});
网络服务
@GET
@Produces("application/json")
@Consumes("application/json")
@Path("checkuser/")
public string getCheckUser(@QueryParam("userId") String userId, @QueryParam("pwd") String pwd){
try{
if(port != null){
boolean result = port.checkUser(userId, pwd);
return new java.lang.Boolean(result).toString();
}
}catch(Exception ex){
//TODO
}
return null;
}
userId 和 pwd 始终为 null
使用 Firebug 我可以看到数据包含
Object{
userId="aaa",
pwd="aa"
}
我也尝试使用JSON.stringify 发送这些数据:
"{"userId":"aaa","pwd":"aa"}"
【问题讨论】:
-
使用带有字段 userId 和 pwd 的对象,而不是
@QueryParam注释。消费 json 时不要使用。 -
这是由 NetBeans 自动生成的。是否有另一种方法可以通过指定参数来发送 json?不是通过对象?
-
object 将是一个 java 类对象,其字段将映射到 json 的字段。你在用什么
rest库? -
如果我理解正确,一方面你说数据发送正确,但在另一个后端说数据值为空。我会尝试打开我的浏览器开发工具并在网络选项卡中检查您在请求中实际发送到后端的内容。它帮助了我很多次:)
标签: java angularjs json web-services rest