Last active
March 12, 2020 16:01
-
-
Save gdonega/bf6908014e49f12a226310aa878fc239 to your computer and use it in GitHub Desktop.
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
package com.testes.spring.exemplo.empresas.services; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import org.springframework.web.client.RestTemplate; | |
import com.mongodb.BasicDBObject; | |
import com.testes.spring.exemplo.empresas.models.EmpresaEnvelope; | |
import com.testes.spring.exemplo.empresas.repositories.EmpresaEnvelopeRepository; | |
@Service | |
public class EmpresaEnvelopeService { | |
@Autowired | |
private EmpresaEnvelopeRepository empresaEnvelopeRepository; | |
public void salvarCnpj(String cnpj) { | |
/*---------------------- Fazendo a requisição ----------------------*/ | |
// Instancia o objeto que | |
RestTemplate restTemplate = new RestTemplate(); | |
// Seta a url base para pesquisar o cnpj | |
String url = "https://www.receitaws.com.br/v1/cnpj/"; | |
// Adiciona o cnpj que deve ser pesquisado | |
url += cnpj; | |
// Executa a pesquisa e pede como retorno o BasicDBObject | |
BasicDBObject jsonApi = restTemplate.getForObject(url, BasicDBObject.class); | |
/*---------------------- Gerando Envelope ----------------------*/ | |
// Gera um objeto de EmpresaEnvelope | |
EmpresaEnvelope empresaEnvelope = new EmpresaEnvelope(); | |
// Processa as informações | |
empresaEnvelope.processarEnvelope(jsonApi); | |
/*---------------------- Persistindo os dados ----------------------*/ | |
empresaEnvelopeRepository.save(empresaEnvelope); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment