vendredi 13 mars 2015

Json value missing between Spring Controller and Javascript variable

I have a Spring boot Server which has an Controller that sends objects to my html. In this html, javascript receives that.


This is my Controller method which sends the object:




@RequestMapping(value = "index", method=RequestMethod.GET)
public ModelAndView index(Principal p) {
ModelAndView mv = new ModelAndView("mapa_credito");
mv.addObject("clientes_banco",Lists.newArrayList(CreditoClientesBanco.findAll()));
mv.addObject("seguimento_cliente", new seguimento_cliente());
return mv;
}


CreditoClientesBanco is an CrudRepository which manages the Credito_clientes_banco. So, I am sending an ArrayList with all the CreditoClientesBanco


This CreditoClientesBanco is an Entity with List variables:




@Entity
public class Credito_clientes_banco {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
long id;
@ElementCollection(fetch=FetchType.EAGER)
List<String> Contrato= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Filial= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Codigo_cliente= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Nome_cliente= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Nascimento= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Sexo= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Escolaridade= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Atividade= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Codigo= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Descricao= new ArrayList<String>();
@JsonInclude
@ElementCollection(fetch=FetchType.EAGER)
List<String> Endereco= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Bairro= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Cidade= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> Estado= new ArrayList<String>();
@ElementCollection(fetch=FetchType.EAGER)
List<String> CEP= new ArrayList<String>();
// here all the Gets and Sets...
...}}


So, all was working OK, i was receiving all the data in var clientes_banco = [[${clientes_banco}]]; in javascript. Like this:




var clientes_banco = [{'BF':['','','','',''],'CEP':['60870-130','60870-130'],'atividade':['COM\xC9RCIO','COM\xC9RCIO'],'bairro':['CONJUNTO PALMEIRAS','CONJUNTO PALMEIRAS'],'cidade':['FORTALEZA','FORTALEZA'],'codigo':['0004','0004'],'codigo_cliente':['0000002842','0000002842'],'comentarios':[],'constituicao':['INFORMAL','INFORMAL'],'contrato':['20100902-01','20110825-03'],'data_comentarios':[],'data_ultima_atualizacao':'12/03/2015 13:54:23','descricao':['MERCEARIA','MERCEARIA'],'endereco':['CONJUNTO PALMEIRAS','CONJUNTO PALMEIRAS','Rua Avar\xC3\xA9, 422 - Conjunto Palmeiras, Fortaleza - CE, 60870-130, Brazil','Rua Avar\xC3\xA9, 422 - Conjunto Palmeiras, Fortaleza - CE, 60870-130, Brazil'],'escolaridade':[],'estado':['CE','CE'],'filial':['001','001'],'fone':['8532695186','8532695186'],'gps_position_updated':true,'id':8899,'latitude':-3.842732799999999,'liberacao':['02/09/2010','25/08/2011'],'longitude':-38.5267604,'nascimento':['05/11/1960','05/11/1960'],'nome_cliente':['IZABEL MARIA SOUZA MACIEL','IZABEL MARIA SOUZA MACIEL'],'numero_parcelas':['012','012'],'numero_pessoas':['','','','',''],'renda_familiar':['0,00','0,00'],'renda_per_capita':['0,00','0,00'],'renovacao':['N','S'],'risco':['A','A'],'saldo_devedor':['0,00','0,00'],'sexo':['M','M'],'situacao':['ATIVO','ATIVO'],'valor_financiado':['3.000,00','5.190,75']},{'BF':['',''],'CEP':['60000-000'],'atividade':['COM\xC9RCIO'],'bairro':['CONJUNTO PALMEIRAS'],'cidade':['FORTALEZA'],'codigo':['0003'],'codigo_cliente':['0000005765'],'comentarios':[],'constituicao':['INFORMAL'],'contrato':['20110825-04'],'data_comentarios':[],'data_ultima_atualizacao':'12/03/2015 13:54:23','descricao':['VENDEDOR AMBULANTE'],'endereco':['CONJUNTO PALMEIRAS','Rua Recanto Verde, 1573 - Jangurussu, Fortaleza - CE, 60870-520, Brazil','Rua Recanto Verde, 1573 - Jangurussu, Fortaleza - CE, 60870-520, Brazil'],'escolaridade':[],'estado':['CE'],'filial':['001'],'fone':['',''],'gps_position_updated':true,'id':8900,'latitude':-3.8406036,'liberacao':['25/08/2011'],'longitude':-38.5123654,'nascimento':['12/04/1975'],'nome_cliente':['JANIA MARIA SILVA DE SOUSA'],'numero_parcelas':['004'],'numero_pessoas':['',''],'renda_familiar':['0,00'],'renda_per_capita':['0,00'],'renovacao':['N'],'risco':['H'],'saldo_devedor':['0,00'],'sexo':['F'],'situacao':['ATIVO'],'valor_financiado':['103,10']},{'BF':['',''] ... ETC...


But suddendly, the value endereco is not sent by the Controller (or not caught by the JavaScript).


I tried to add a @JsonInclude above the @ElementCollection(fetch=FetchType.EAGER) List<String> Endereco= new ArrayList<String>(); forcing to appear in the JSON. I tried also changing this mv.addObject("clientes_banco",Lists.newArrayList(CreditoClientesBanco.findAll())); for thismv.addObject("clientes_banco",CreditoClientesBanco.findAll()); with the same error.


Why now the value endereco is missing?


Any idea? Note: In this project I am working with Thymeleaf too.


Aucun commentaire:

Enregistrer un commentaire