Last active
July 3, 2018 01:59
-
-
Save lucaspolo/b8e88293d43ca33ae222 to your computer and use it in GitHub Desktop.
Simples encurtador que requisita a encurtação de um link para o Migre.me e faz parser para um Hash
This file contains 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 br.com.lucaspolo.json.teste; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.naming.LimitExceededException; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.WebTarget; | |
import javax.ws.rs.core.Link; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.type.TypeReference; | |
/** | |
* @author lucaspolo | |
* Classe de teste que faz a requisição do migre.me, encurta a URL e converte em um Hash. | |
* A dica do parser para Map foi retirada de | |
* {@link http://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/} | |
* O POM com as dependencias do Maven para rodar este exemplo é este | |
* {@link https://gist.github.com/lucaspolo/7a2a0ea33f3ce0c93975} | |
*/ | |
public class App | |
{ | |
public static void main( String[] args ) throws Exception | |
{ | |
URI uri = new URI("http://migre.me/api.json?"); | |
Client client = ClientBuilder.newClient(); | |
WebTarget target = client.target(uri.toURL().toString()) | |
.queryParam("url", "http://www.ocaminhodoprogramador.com.br"); | |
String conteudo = target.request().get(String.class); | |
System.out.println(conteudo); | |
Map<String, String> map = new HashMap<>(); | |
ObjectMapper mapper = new ObjectMapper(); | |
map = mapper.readValue(conteudo, new TypeReference<HashMap<String, String>>() {}); | |
System.out.println(map.get("migre")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment