JSON list in Angularjs not displayed

Asked

Viewed 188 times

1

I’m trying to return a list of a Java webservice, but on the HTML page it is blank, I do not see my error, below the code.

@Path("/contatos")
public class ContatoResource {

		static private Map<Integer, Contato> contatosMap;

		static {
			contatosMap = new HashMap<Integer, Contato>();
			
			Contato b1 = new Contato();
			b1.setId(1);
			b1.setNome("Rodrigo");
			b1.setFone("9999-9999");

			contatosMap.put(b1.getId(), b1);

			Contato b2 = new Contato();
			b2.setId(2);
			b2.setNome("Ana");
			b2.setFone("8888-8888");

			contatosMap.put(b2.getId(), b2);
		}

		
		
		@GET
		@Produces("application/json")
		public List<Contato> getContatos() {
			return new ArrayList<Contato>(contatosMap.values());
		}

var app = angular.module("listaTelefonica", []);
	app.controller("listaTelefonicaCtrl", function($scope,$http){
		$scope.titulo = "Lista Telefônica";
		$scope.contatos=[];

		$scope.operadoras=["oi","tim","vivo","claro"]
		$scope.adicionarContato=function(contato){
			$scope.contatos.push(angular.copy(contato));
			delete $scope.contato;
		}

		$scope.carregarContato = function () {
			$http.get("http://localhost:8080/AngularJS/rest/contatos").success(function (data) {
				$scope.contatos = data;
			});
		};

		$scope.carregarContato();
 })

<table class="table table-striped table-hover" ng-show="contatos.length>0">
			<tr>
				<th></th>
				<th>Nome</th>
				<th>Telefone</th>
				<th>Operadoras</th>
			</tr>
			<tr ng-repeat="contato in contatos" ng-class="{selecionado:contato.selecionado}">
				<td><input type="checkBox" ng-model="contato.selecionado" /></td>
				<td>{{contato.nome | uppercase}}</td>
				<td>{{contato.fone}}</td>
				<td>{{contato.operadora}}</td>
			</tr>
		</table>

1 answer

1

Angular produces and consumes Json objects in its http methods. Therefore, you need to annotate in your Contactource class:

@Path("/contatos")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ContatoResource {

Browser other questions tagged

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