How to retrieve the value of the return boolean of a REST service in Angularjs?

2

I have a Java / Jersey service that returns a Boolean pro for AngularJs. In my controller, I get the value of the return from the promise, but it returns me an Object. In other cases, as a return from a User entity, for example, it works fine, because I get the attributes of the returned object. Now when it returns only a boolean, how do I recover the value?

Below is an example I'm using:

Java service:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/verificaEmail")
public Boolean verificarEmail(String email){

    SimpleEntityManager simpleEntityManager = new SimpleEntityManager(persistenceUnitName);

    IUsuarioBusiness usuarioB = new UsuarioBusiness(simpleEntityManager);

    Boolean result = usuarioB.verificaEmail(email);

    return result;

}

Angular Controller:

this.cadastrarUsuario = function() {

  var usuario = {
    email: this.usuarioLogin.email,
    senha: this.usuarioLogin.senha,
    nome: this.usuarioLogin.nome
  }

  var verificaEmail = VerificaEmail.email(usuario.email);

  verificaEmail.$promise.then( 
    function success(result)
    {
      if (result) {
        $scope.erro = true;
        $scope.mensagemErro = "Email já existe!";
        $scope.mostraMensagem = true;
        $scope.setClasseErro();
      }
    })};

Service:

var servicoLogin = angular.module('servicoLogin', ['ngResource']);

servicoLogin.factory('VerificaEmail', ['$resource',
  function($resource){
    return $resource('http://localhost:8081/GameService/webserver/usuario/verificaEmail', {}, {
      email: {method:'POST'}
    });
  }]);

I'm just asking for help because I've already broken my head and I have not been able to solve this problem that seems to be simple.

    
asked by anonymous 06.11.2015 / 02:00

2 answers

1

Your problem seems to me to be even using ngResource , this module was made to consume RESTfull services, and so it seems to me to be waiting for the response coming from the WebService to be an object, when it receives a boolean it can not properly handle this value and it is not passed to its function.

I would say your alternatives are to not use ngResource and make the request directly using $http , or else encapsulate your response into a server-side object.

    
06.11.2015 / 19:44
0

You can solve by putting the return on a map or in Object:

Map<String, Object> resposta = new HashMap<String, Object>();
resposta.put("resultado", Boolean.TRUE);
    
26.10.2016 / 10:54