Created
July 10, 2018 16:36
-
-
Save nosrednawall/0c58cf160893acd3351fc076a0c8b2f2 to your computer and use it in GitHub Desktop.
Classe util que busca o cep informado na API VIACEP, funciona bem no java 1.8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.util.Arrays; | |
| import java.util.HashMap; | |
| import java.util.HashSet; | |
| import java.util.Iterator; | |
| import java.util.Map; | |
| import java.util.Map.Entry; | |
| import java.util.Set; | |
| import javax.json.Json; | |
| import javax.json.JsonObject; | |
| import org.apache.http.HttpEntity; | |
| import org.apache.http.HttpResponse; | |
| import org.apache.http.client.methods.HttpGet; | |
| import org.apache.http.impl.client.CloseableHttpClient; | |
| import org.apache.http.impl.client.HttpClientBuilder; | |
| import com.fasterxml.jackson.annotation.JsonValue; | |
| /** | |
| * https://github.com/uliss3s/ceputil Classe para recuperar informações do WS do | |
| * viacep.com.br | |
| */ | |
| public class BuscaCepWSUtil { | |
| private static final Set<String> CAMPOS = new HashSet<String>( | |
| Arrays.asList("cep", "logradouro", "complemento", "bairro", "localidade", "uf", "unidade", "ibge", "gia")); | |
| /** | |
| * Recupera objeto Endereco pelo CEP | |
| * | |
| * @param cep | |
| * String no formato 00000000 | |
| * @return instancia de br.com.viacep.Endereco | |
| */ | |
| public static Endereco getEnderecoPorCep(String cep) { | |
| JsonObject jsonObject = getCepResponse(cep); | |
| Endereco endereco = null; | |
| JsonValue erro = (JsonValue) jsonObject.get("erro"); | |
| if (erro == null) { | |
| endereco = new Endereco(); | |
| endereco.setCep(jsonObject.getString("cep")); | |
| endereco.setLogradouro(jsonObject.getString("logradouro")); | |
| endereco.setBairro(jsonObject.getString("bairro")); | |
| endereco.setMunicipio(jsonObject.getString("localidade")); | |
| endereco.setEstadoRecebendoEmString(jsonObject.getString("uf")); | |
| } | |
| return endereco; | |
| } | |
| /** | |
| * Recupera Map<String,String> pelo CEP | |
| * | |
| * @param cep | |
| * String no formato 00000000 | |
| * @return instancia de Map<String,String> | |
| */ | |
| public static Map<String, String> getMapPorCep(String cep) { | |
| JsonObject jsonObject = getCepResponse(cep); | |
| JsonValue erro = (JsonValue) jsonObject.get("erro"); | |
| Map<String, String> mapa = null; | |
| if (erro == null) { | |
| mapa = new HashMap<String, String>(); | |
| for (Iterator<Entry<String, javax.json.JsonValue>> it = jsonObject.entrySet().iterator(); it.hasNext();) { | |
| Entry<String, javax.json.JsonValue> entry = it.next(); | |
| mapa.put(entry.getKey(), entry.getValue().toString()); | |
| } | |
| } | |
| return mapa; | |
| } | |
| /** | |
| * Método recebe o cep e faz a requisição no web service | |
| * | |
| * @param cep | |
| * @return retorna o response do web service | |
| */ | |
| private static JsonObject getCepResponse(String cep) { | |
| JsonObject responseJO = null; | |
| try { | |
| CloseableHttpClient httpclient = HttpClientBuilder.create().build(); | |
| HttpGet httpGet = new HttpGet("https://viacep.com.br/ws/" + cep + "/json"); | |
| HttpResponse response; | |
| response = httpclient.execute(httpGet); | |
| HttpEntity entity = response.getEntity(); | |
| responseJO = Json.createReader(entity.getContent()).readObject(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| return responseJO; | |
| } | |
| public static Set<String> getCampos() { | |
| return CAMPOS; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment