No HTML, não estou conseguindo ler esta classe Rest e mostrar em um select de pais. Ao selecionar um país mostra os estados. Ao selecionar um estado, mostra os municípios e assim por diante.
javascript html
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('http://localhost:8080/desif/buscaPais', function (data) {
var items = [];
var options = '<option value="">escolha um país</option>';
$.each(data, function (key, val) {
options += '<option value="' + val.nome + '">' + val.nome + '</option>';
});
$("#estados").html(options);
$("#estados").change(function () {
var options_cidades = '';
var str = "";
$("#estados option:selected").each(function () {
str += $(this).text();
});
$.each(data, function (key, val) {
if(val.nome == str) {
$.each(val.cidades, function (key_city, val_city) {
options_cidades += '<option value="' + val_city + '">' + val_city + '</option>';
});
}
});
$("#cidades").html(options_cidades);
}).change();
});
});
</script>
<!-- Estado -->
<select id="estados">
<option value=""></option>
</select>
<select id="cidades">
</select>
Classes em java
Pais e estado
@RestController
public class EnderecoServicoRest {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private PaisServico paisServico;
@Autowired
private EstadoServico estadoServico;
@Autowired
private MunicipioServico municipioServico;
@Autowired
private BairroServico bairroServico;
@Autowired
private LogradouroServico logradouroServico;
@RequestMapping(value = "/buscaPais", method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<PaisRestDTO>> buscaPais() {
try {
List<PaisEntity> listaPaises = null;
listaPaises = paisServico.listar();
List<PaisRestDTO> listaPaisesRest = new ArrayList<PaisRestDTO>();
for (PaisEntity pais : listaPaises) {
PaisRestDTO dto = new PaisRestDTO();
dto.setDescricao(pais.getDescricao());
dto.setId(pais.getId());
listaPaisesRest.add(dto);
}
return new ResponseEntity<List<PaisRestDTO>>(listaPaisesRest, HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
@RequestMapping(value = "/buscaEstadoPorPais/{pais}", method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<EstadoRestDTO>> buscaEstadoPorPais(@PathVariable Long pais) {
try {
List<EstadoEntity> listaEstados = null;
PaisEntity paisEntidade = new PaisEntity();
paisEntidade.setId(pais);
listaEstados = estadoServico.buscaEstadoPorPais(paisEntidade);
List<EstadoRestDTO> listaEstadosRest = new ArrayList<EstadoRestDTO>();
for (EstadoEntity estado : listaEstados) {
EstadoRestDTO dto = new EstadoRestDTO();
dto.setAbreviacao(estado.getAbreviacao());
dto.setDescricao(estado.getDescricao());
dto.setId(estado.getId());
dto.setPais(estado.getPais().getId());
listaEstadosRest.add(dto);
}
return new ResponseEntity<List<EstadoRestDTO>>(listaEstadosRest, HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
@RequestMapping(value = "/buscaMunicipioPorPaisEstado/{pais}/{estado}", method = RequestMethod.GET, produces = {
"application/json" })
public ResponseEntity<List<MunicipioRestDTO>> buscaMunicipioPorPaisEstado(@PathVariable Long pais,
@PathVariable Long estado) {
try {
List<MunicipioEntity> listaMunicipios = null;
PaisEntity paisEntidade = new PaisEntity();
paisEntidade.setId(pais);
EstadoEntity estadoEntidade = new EstadoEntity();
estadoEntidade.setId(estado);
listaMunicipios = municipioServico.buscaMunicipioPorPaisEstado(paisEntidade, estadoEntidade);
List<MunicipioRestDTO> listaMunicipiosRest = new ArrayList<MunicipioRestDTO>();
for (MunicipioEntity municipio : listaMunicipios) {
MunicipioRestDTO dto = new MunicipioRestDTO();
dto.setDescricao(municipio.getDescricao());
dto.setId(municipio.getId());
dto.setPais(municipio.getPais().getId());
dto.setEstado(municipio.getEstado().getId());
listaMunicipiosRest.add(dto);
}
return new ResponseEntity<List<MunicipioRestDTO>>(listaMunicipiosRest, HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
@RequestMapping(value = "/buscaBairroPorPaisEstadoMunicipio/{pais}/{estado}/{municipio}", method = RequestMethod.GET, produces = {
"application/json" })
public ResponseEntity<List<BairroRestDTO>> buscaBairroPorPaisEstadoMunicipio(@PathVariable Long pais,
@PathVariable Long estado, @PathVariable Long municipio) {
try {
List<BairroEntity> listaBairros = null;
PaisEntity paisEntidade = new PaisEntity();
paisEntidade.setId(pais);
EstadoEntity estadoEntidade = new EstadoEntity();
estadoEntidade.setId(estado);
MunicipioEntity municipioEntidade = new MunicipioEntity();
municipioEntidade.setId(municipio);
listaBairros = bairroServico.buscaBairroPorPaisEstadoMunicipio(paisEntidade, estadoEntidade,
municipioEntidade);
List<BairroRestDTO> listaBairrosRest = new ArrayList<BairroRestDTO>();
for (BairroEntity bairro : listaBairros) {
BairroRestDTO dto = new BairroRestDTO();
dto.setDescricao(bairro.getDescricao());
dto.setId(bairro.getId());
dto.setPais(bairro.getPais().getId());
dto.setEstado(bairro.getEstado().getId());
dto.setMunicipio(bairro.getMunicipio().getId());
listaBairrosRest.add(dto);
}
return new ResponseEntity<List<BairroRestDTO>>(listaBairrosRest, HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
@RequestMapping(value = "/buscaLogradouroPorPaisEstadoMunicipioBairro/{pais}/{estado}/{municipio}/{bairro}", method = RequestMethod.GET, produces = {
"application/json" })
public ResponseEntity<List<LogradouroRestDTO>> buscaLogradouroPorPaisEstadoMunicipioBairro(@PathVariable Long pais,
@PathVariable Long estado, @PathVariable Long municipio, @PathVariable Long bairro) {
try {
List<LogradouroEntity> listaLogradouros = null;
PaisEntity paisEntidade = new PaisEntity();
paisEntidade.setId(pais);
EstadoEntity estadoEntidade = new EstadoEntity();
estadoEntidade.setId(estado);
MunicipioEntity municipioEntidade = new MunicipioEntity();
municipioEntidade.setId(municipio);
BairroEntity bairroEntidade = new BairroEntity();
bairroEntidade.setId(bairro);
listaLogradouros = logradouroServico.buscaLogradouroPorPaisEstadoMunicipioBairro(paisEntidade,
estadoEntidade, municipioEntidade, bairroEntidade);
List<LogradouroRestDTO> listaLogradouroRest = new ArrayList<LogradouroRestDTO>();
for (LogradouroEntity logradouro : listaLogradouros) {
LogradouroRestDTO dto = new LogradouroRestDTO();
dto.setDescricao(logradouro.getDescricao());
dto.setId(logradouro.getId());
dto.setPais(logradouro.getPais().getId());
dto.setEstado(logradouro.getEstado().getId());
dto.setMunicipio(logradouro.getMunicipio().getId());
dto.setBairro(logradouro.getBairro().getId());
listaLogradouroRest.add(dto);
}
return new ResponseEntity<List<LogradouroRestDTO>>(listaLogradouroRest, HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
}