2014-10-18 26 views
1

我试图在响应中返回一个布尔值。但它回来我的AngularJS应用程序中的字符数组:Spring REST将布尔实体返回给AngularJS ...以字符数组的形式出现

资源{0:“f”,1:“a”,2:“l”,3:“s”,4:“e”, $承诺:对象,$解决:真实,$得到:函数,$保存:函数,$查询:函数...}

这里是控制器:

@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 /项目,其余/技术/ 4 /依赖性 请求方法:GET 状态代码:200 OK 响应头文查看源代码 Access-Control-Allow-Headers:Accept-Language,Content-Type,X-Requested-With,accept,Origin,访问控制请求方法,访问控制请求-Headers,Authorization Access-Control-Allow-Methods:POST,PUT,GET,OPTIONS,DELETE Access-Control-Allow-Origin:xxxxx:9000 Access-Control-Max-Age:3600 Cache-Control:no-cache,no-store,max-age = 0,必须重新验证 Content-Type:application/json; charset = UTF-8 Date:Sun,19 Oct 2014 07:47:59 GMT Expires:0 Pragma:no-cache 服务器:Apache-Coyote/1.1 Strict-Transport-Security:max-age = 31536000;包括子域 传输编码:分块 X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1;模式=块

而网络响应选项卡显示: 假

+0

请显示您的角码。 – Vaelyr 2014-10-18 21:56:31

回答

2

我使出有一个包装类:

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); 
}); 
相关问题