2
votes

I'm trying to return a boolean value in a response. But it comes back as an array of characters in my AngularJS app:

Resource {0: "f", 1: "a", 2: "l", 3: "s", 4: "e", $promise: Object, $resolved: true, $get: function, $save: function, $query: function…}

Here is the controller:

@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);
    }
}

Same response with this controller:

@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);
    }
}

Here is the AngularJS code:

'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;
    });
  }
]);

In the Chromium browser debug pane, the network Headers tab shows: Remote Address:127.0.0.1:8443 Request URL:xxxxx:8443/project-rest/technologies/4/dependencies Request Method:GET Status Code:200 OK Response Headersview source Access-Control-Allow-Headers:Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-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, must-revalidate Content-Type:application/json;charset=UTF-8 Date:Sun, 19 Oct 2014 07:47:59 GMT Expires:0 Pragma:no-cache Server:Apache-Coyote/1.1 Strict-Transport-Security:max-age=31536000 ; includeSubDomains Transfer-Encoding:chunked X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode=block

And the network Response tab shows: false

1
Show your angular code please. – Vaelyr

1 Answers

3
votes

I resorted to have a wrapper class:

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;
    }

}

And now I can return a response entity:

return new ResponseEntity<ValueResource>(new ValueResource(hasDependencies), responseHeaders, HttpStatus.OK);

Which gives me a boolean value:

TechnologyService.hasDependencies($stateParams.technologyId, function(data) {
  console.log("hasDependencies", data.value);
});