Skip to content

Instantly share code, notes, and snippets.

@vacax
Created March 28, 2019 22:16
Show Gist options
  • Save vacax/27ba0cab40fe03053244972861b358ea to your computer and use it in GitHub Desktop.
Save vacax/27ba0cab40fe03053244972861b358ea to your computer and use it in GitHub Desktop.
Ejemplo Unirest serializando objeto en el cliente
public static void main(String[] args) throws Exception {
System.out.println("Cliente REST - Hola Mundo :-D - Unirest");
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://localhost:4567/rest/estudiantes/0")
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.queryString("apiKey", "asdasgdasgd")
.asJson();
if(jsonResponse.getStatus()==200){
JsonNode objeto = jsonResponse.getBody();
System.out.println(String.format("matricula : %d, nombre: %s, Carrera: %s",
objeto.getObject().get("matricula"),
objeto.getObject().get("nombre"),
objeto.getObject().get("carrera")));
} else {
System.out.println("Error en al consulta: "+jsonResponse.getStatus());
JSONObject object = jsonResponse.getBody().getObject();
System.out.println(String.format("codigo: %d, mensaje: %s", object.get("codigo"), object.get("mensaje")));
}
// Mapeador para trabajar con las conversione de objetos JSON.
Unirest.setObjectMapper(new ObjectMapper() {
private Gson gson = new Gson();
public <T> T readValue(String value, Class<T> valueType) {
try {
return gson.fromJson(value, valueType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String writeValue(Object value) {
try {
return gson.toJson(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
HttpResponse<Estudiante> jsonResponseEstudiante = Unirest.get("http://localhost:4567/rest/estudiantes/1")
.header("Content-Type", "application/json")
.header("auth", "asdasdasdadas")
.header("Accept", "application/json")
.queryString("apiKey", "asdasgasdasddasgd")
.asObject(Estudiante.class);
if(jsonResponseEstudiante.getStatus()==200){
Estudiante objeto = jsonResponseEstudiante.getBody();
System.out.println(String.format("Parseando el objeto matricula : %d, nombre: %s, Carrera: %s",
objeto.getMatricula(),
objeto.getNombre(),
objeto.getCarrera()));
} else {
System.out.println("Error en al consulta: "+jsonResponseEstudiante.getStatus());
InputStream rawBody = jsonResponseEstudiante.getRawBody();
System.out.println(rawBody);
//JSONObject object = new JSONObject().
//System.out.println(String.format("codigo: %d, mensaje: %s", object.get("codigo"), object.get("mensaje")));
}
}
static class Estudiante{
int matricula;
String nombre;
String carrera;
public int getMatricula() {
return matricula;
}
public void setMatricula(int matricula) {
this.matricula = matricula;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getCarrera() {
return carrera;
}
public void setCarrera(String carrera) {
this.carrera = carrera;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment