How to recover the value of Boolean from a REST service in Angularjs?

Asked

Viewed 812 times

2

I have a Java/Jersey service that returns a Boolean pro Angularjs. In my controller, I take the value of the return of the Promise, but it returns me an Object. In other cases, such as a return of an User entity, for example, works well, because I take the attributes of the returned object. Now when only a Boolean returns, 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;

}

Controller Angularjs:

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 only asking for help because I’ve already broken my head and I can’t solve this problem that seems to be simple.

  • Post the json being returned

  • How is this function VerificaEmail.email(usuario.email)? She uses the $http to make the request?

  • @Leandrogodoyrosa, I am using the angular-Resource. I put the function in the body of the question.

  • @jbueno the return is a json with only "false" or "true"

  • If result is always "true" or "false", the if(result) is always true (because you are dealing with strings in JS).

2 answers

1

Your problem seems to be the use of ngResource, this module was made to consume Restfull services, and so it seems to me that the response coming from Webservice is an object, when it receives a boolean it cannot properly treat this value and it does not get to be passed to its function.

I would say your alternatives are not to use the ngResource and request directly using $http, or else encapsulate your response in an object on the server side.

  • I ended up choosing to create an object on the server to make it work. I just find it strange that ngResource does not support this type of feedback, but since I did not find anything on the internet dealing with this case, it goes in the easiest way rs. Anyway, thanks for your help!

  • @Eduardomartins this behavior is exactly as expected. As Leandro commented, REST predicts returns of objects in JSON notation (ex. {'resultado':'true'}), not primitive values (true). In this case, use $http.

0

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

Map<String, Object> resposta = new HashMap<String, Object>();
resposta.put("resultado", Boolean.TRUE);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.