Spring REST将布尔实体返回到AngularJS。。。以字符数组的形式出现
我试图在响应中返回一个布尔值。但在我的AngularJS应用程序中,它是一个字符数组: 资源{0:f',1:a',2:l',3:s',4:e',$promise:Object,$resolved:true,$get:function,$save:function,$query:function…} 这是控制器:Spring REST将布尔实体返回到AngularJS。。。以字符数组的形式出现,angularjs,spring,rest,Angularjs,Spring,Rest,我试图在响应中返回一个布尔值。但在我的AngularJS应用程序中,它是一个字符数组: 资源{0:f',1:a',2:l',3:s',4:e',$promise:Object,$resolved:true,$get:function,$save:function,$query:function…} 这是控制器: @RequestMapping(value = RESTConstants.SLASH + "{id}" + RESTConstants.SLASH + RESTConstants.DE
@RequestMapping(value = RESTConstants.SLASH + "{id}" + RESTConstants.SLASH + RESTConstants.DEPENDENCIES, method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Boolean> hasDependencies(@PathVariable Long id, UriComponentsBuilder builder) {
HttpHeaders responseHeaders = new HttpHeaders();
Technology technology = technologyService.findById(id);
if (technology == null) {
return new ResponseEntity<Boolean>(responseHeaders, HttpStatus.NOT_FOUND);
} else {
Boolean hasDependencies = technologyService.hasDependencies(id);
responseHeaders.setLocation(builder.path(RESTConstants.SLASH + RESTConstants.TECHNOLOGIES + RESTConstants.SLASH + "{id}" + RESTConstants.DEPENDENCIES).buildAndExpand(technology.getId()).toUri());
return new ResponseEntity<Boolean>(hasDependencies, responseHeaders, HttpStatus.OK);
}
}
@RequestMapping(value = RESTConstants.SLASH + "{id}" + RESTConstants.SLASH + RESTConstants.DEPENDENCIES, method = RequestMethod.GET)
@ResponseBody
public boolean hasDependencies(@PathVariable Long id, UriComponentsBuilder builder) {
Technology technology = technologyService.findById(id);
if (technology == null) {
return false;
} else {
return technologyService.hasDependencies(id);
}
}
以下是AngularJS代码:
'use strict';
utilsModule.factory('RESTService',
['$resource', 'ENV', 'FileUploader', 'AuthService',
function($resource, ENV, FileUploader, AuthService) {
return {
Technology: $resource(ENV.NITRO_PROJECT_REST_URL + '/technologies/:technologyId', {}, {
hasDependencies: {
url: ENV.NITRO_PROJECT_REST_URL + '/technologies/:technologyId/dependencies',
method: 'GET',
params: { technologyId: 'technologyId' }
}
})
}
}
]);
'use strict';
technologyModule.factory('TechnologyService',
['RESTService',
function(RESTService) {
var factory = {};
factory.hasDependencies = function(technologyId, callback) {
return RESTService.Technology.hasDependencies({technologyId: technologyId}).$promise.then(callback);
}
return factory;
}
]);
'use strict';
technologyModule.controller('technology.deleteCtrl',
['$scope', '$state', '$stateParams', 'TechnologyService',
function($scope, $state, $stateParams, TechnologyService) {
$scope.hasDependencies = true;
TechnologyService.hasDependencies($stateParams.technologyId, function(hasDependencies) {
console.log("hasDependencies", hasDependencies);
$scope.hasDependencies = hasDependencies;
});
}
]);
在Chromium浏览器调试窗格中,网络标题选项卡显示:
远程地址:127.0.0.1:8443
请求URL:xxxxx:8443/project rest/technologies/4/dependencies
请求方法:获取
状态代码:200 OK
响应头视图源
访问控制允许标头:接受语言、内容类型、X-Requested-With、接受、来源、访问控制请求方法、访问控制请求标头、授权
访问控制允许方法:POST、PUT、GET、OPTIONS、DELETE
访问控制允许来源:xxxxx:9000
访问控制最大年龄:3600
缓存控制:无缓存,无存储,最大年龄=0,必须重新验证
内容类型:application/json;字符集=UTF-8
日期:2014年10月19日星期日07:47:59 GMT
过期日期:0
Pragma:没有缓存
服务器:ApacheCoote/1.1
严格的交通安全:最大年龄为31536000岁;包含子域
传输编码:分块
X-Content-Type-Options:nosniff
X帧选项:拒绝
X-XSS-Protection:1;模式=块
“网络响应”选项卡显示:
false我求助于使用包装器类:
public class ValueResource {
private Object value;
public ValueResource(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
现在我可以返回一个响应实体:
return new ResponseEntity<ValueResource>(new ValueResource(hasDependencies), responseHeaders, HttpStatus.OK);
请出示你的角度代码。
TechnologyService.hasDependencies($stateParams.technologyId, function(data) {
console.log("hasDependencies", data.value);
});