Web services 使用RESTful Web服务的Traverson库抛出JSONPath错误
我目前正在尝试使用traverson从SpringBoot提供的REST接口获取一些信息 我现在的目标是让特拉弗森沿着Web services 使用RESTful Web服务的Traverson库抛出JSONPath错误,web-services,rest,spring-boot,Web Services,Rest,Spring Boot,我目前正在尝试使用traverson从SpringBoot提供的REST接口获取一些信息 我现在的目标是让特拉弗森沿着 http://localhost/users并使用此代码获取信息 traverson.from('http://localhost:8090') .json() .follow('$._links.user') .getResource(function(err, resource){ if(err){ conso
http://localhost/users并使用此代码获取信息
traverson.from('http://localhost:8090')
.json()
.follow('$._links.user')
.getResource(function(err, resource){
if(err){
console.log(err);
return;
}
console.log(resource);
});
调用端点时返回的json结构如下所示:
{
"_links" : {
"ressource" : {
"href" : "http://localhost:8090/ressource{?page,size,sort}",
"templated" : true
},
"user" : {
"href" : "http://localhost:8090/user{?page,size,sort}",
"templated" : true
},
"alert" : {
"href" : "http://localhost:8090/alert{?page,size,sort}",
"templated" : true
}
}
不幸的是,这会导致一个错误:
Uncaught TypeError: a.step.url.search is not a function
但是,如果我只获取端点而不使用JSONPath语法,它将为我提供所提到的结构。
当我弄乱JSONPath表达式时,它将产生一个更合理的错误:
[Error: JSONPath expression $.links.user returned no match in document:
{"_links":{"ressource":{"href":"http://localhost:8090/ressource{?page,size,sort}
","templated":true},"user":{"href":"http://localhost:8090/user{?page,size,sort}"
,"templated":true},"alert":{"href":"http://localhost:8090/alert{?page,size,sort}
","templated":true}}]
可能是我错过了一些非常明显的东西,这会很好
至少感谢您阅读到目前为止:)它可能与谁有关,
经过一些尝试和错误之后,您必须非常小心您提供的JSON路径表达式
traverson.from('http://localhost:8090')
.json()
.follow('$._links.user.href')
.getResource(function(err, resource){
if(err){
console.log(err);
return;
}
console.log(resource);
});
这将在以下情况下起作用:
traverson.from('http://localhost:8090')
.json()
.follow('$._links.user')
.getResource(function(err, resource){
if(err){
console.log(err);
return;
}
console.log(resource);
});
不会。
原因似乎是$.\u links.user指向的是一个对象,而不是对象属性(请参见上面的端点结构)。这会导致traverson库中的求值函数产生某种难以理解的错误
让我们希望有人能利用这些信息,不要花上几个小时思考这个问题
编辑1:向库的所有者填写问题后,此问题现在应该不再出现