Example usage for org.springframework.web.client RestTemplate getMessageConverters

List of usage examples for org.springframework.web.client RestTemplate getMessageConverters

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate getMessageConverters.

Prototype

public List<HttpMessageConverter<?>> getMessageConverters() 

Source Link

Document

Return the list of message body converters.

Usage

From source file:org.messic.android.util.RestJSONClient.java

/**
 * Rest GET petition to the server at the url param, sending all the post parameters defiend at formData. This post
 * return an object (json marshalling) of class defined at clazz parameter. You should register a
 * {@link RestListener} in order to obtain the returned object, this is because the petition is done in an async
 * process.//from   www .ja v  a  2  s.co m
 * 
 * @param url {@link string} URL to attack
 * @param clazz Class<T/> class that you will marshall to a json object
 * @param rl {@link RestListener} listener to push the object returned
 */
public static <T> void get(final String url, final Class<T> clazz, final RestListener<T> rl) {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
    final HttpEntity<MultiValueMap<?, ?>> requestEntity = new HttpEntity<MultiValueMap<?, ?>>(
            new LinkedMultiValueMap<String, Object>(), requestHeaders);

    AsyncTask<Void, Void, Void> at = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, clazz);
                rl.response(response.getBody());
            } catch (Exception e) {
                rl.fail(e);
            }
            return null;
        }

    };

    at.execute();
}

From source file:org.messic.android.util.RestJSONClient.java

/**
 * Rest POST petition to the server at the url param, sending all the post parameters defiend at formData. This post
 * return an object (json marshalling) of class defined at clazz parameter. You should register a
 * {@link RestListener} in order to obtain the returned object, this is because the petition is done in an async
 * process.//from  ww w .  ja v  a2  s  .  c  o  m
 * 
 * @param url {@link string} URL to attack
 * @param formData {@link MultiValueMap}<?,?/> map of parameters to send
 * @param clazz Class<T/> class that you will marshall to a json object
 * @param rl {@link RestListener} listener to push the object returned
 */
public static <T> void post(final String url, MultiValueMap<?, ?> formData, final Class<T> clazz,
        final RestListener<T> rl) {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    // Sending multipart/form-data
    requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
    final HttpEntity<MultiValueMap<?, ?>> requestEntity = new HttpEntity<MultiValueMap<?, ?>>(formData,
            requestHeaders);

    AsyncTask<Void, Void, Void> at = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, clazz);
                rl.response(response.getBody());
            } catch (Exception e) {
                rl.fail(e);
            }
            return null;
        }

    };

    at.execute();
}

From source file:cz.cvut.jirutjak.fastimport.droid.oauth2.AccessTokenUtils.java

public static RestTemplate createRestTemplate() {
    RestTemplate template = new RestTemplate();
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    template.getMessageConverters().add(new GsonHttpMessageConverter(gson));

    return template;
}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

public static void mostrarContenedor(String codigo) {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService") + "restcontenedor/"
            + codigo + ".htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    Contenedor c = restTemplate1.getForObject(url, Contenedor.class);
    System.out.println("\tId \t\tCodigo \t\tMarca \t\tModelo \t\tCapacidad(kgs)");
    System.out.println("\t" + String.valueOf(c.getId()) + "\t\t" + c.getCodigo() + " \t\t" + c.getMarca()
            + " \t\t" + c.getModelo() + " \t\t" + String.valueOf(c.getCapacidad()));

}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

public static void profilingMasLento() {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService")
            + "restprofiling/lento.htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    String[] lento = restTemplate1.getForObject(url, String[].class);

    System.out.println("\tOperacion \t\tTiempo");
    System.out.println("\t" + lento[0] + "\t\t" + lento[1]);

}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

public static void profilingMasRapido() {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService")
            + "restprofiling/rapido.htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    String[] rapido = restTemplate1.getForObject(url, String[].class);

    System.out.println("\tOperacion \t\tTiempo");
    System.out.println("\t" + rapido[0] + "\t\t" + rapido[1]);

}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

public static void listarContenedores() {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService")
            + "restcontenedor/contenedores.htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    Contenedor[] contenedores = restTemplate1.getForObject(url, Contenedor[].class);
    System.out.println("\tId \t\tCodigo \t\tMarca \t\tModelo \t\tCapacidad(kgs)");
    for (Contenedor c : contenedores) {
        System.out.println("\t" + c.getId() + "\t\t" + c.getCodigo() + " \t\t" + c.getMarca() + " \t\t"
                + c.getModelo() + "\t\t" + String.valueOf(c.getCapacidad()));
    }//from   w w  w  . ja va2s  . c  o  m
}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

public static void listarBarcos() {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService") + "restbarco/barcos.htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    Barco[] barcos = restTemplate1.getForObject(url, Barco[].class);
    System.out.println(//ww  w  .j  av a 2  s . c  o m
            "\tId \t\tCodigo \t\tNombre \t\tBandera \t\tCapacidad(kgs) \t\tAo \t\tCantidadTripulantes");
    for (Barco barco : barcos) {
        System.out.println("\t" + barco.getId() + "\t\t" + barco.getCodigo() + " \t\t" + barco.getNombre()
                + " \t\t" + barco.getBandera() + " \t\t" + barco.getCapacidadTransporte() + " \t\t"
                + String.valueOf(barco.getAnioFabricacion()) + " \t\t" + barco.getCantidadTripulantes());
    }
}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

public static void profilingPromedios() {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService")
            + "restprofiling/promedios.htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    String promedios = restTemplate1.getForObject(url, String.class);
    System.out.println("\tOperacion \t\tTiempo");
    String[] ss = promedios.split("]");
    for (int i = 0; i < ss.length; i++) {

        System.out.println("\t" + ss[i].replace("[", ""));
    }/*ww  w  .  j  a  v  a2s  . c  o m*/

}

From source file:uy.edu.ort.fachada.FachadaOperaciones.java

/**
 * Operaciones sobre la Entidad Barco//from w  w w.j a  v  a  2 s  . c  o m
 */

public static void mostrarBarco(String codigo) {
    String url = ManejoPropiedades.obtenerInstancia().obtenerPropiedad("restService") + "restbarco/" + codigo
            + ".htm";

    RestTemplate restTemplate1 = new RestTemplate();
    restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate1.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    Barco barco = restTemplate1.getForObject(url, Barco.class);
    System.out.println(
            "\tId \t\tCodigo \t\tNombre \t\tBandera \t\tCapacidad(kgs) \t\tAo \t\tCantidadTripulantes");
    System.out.println("\t" + barco.getId() + "\t\t" + barco.getCodigo() + " \t\t" + barco.getNombre() + " \t\t"
            + barco.getBandera() + " \t\t" + barco.getCapacidadTransporte() + " \t\t"
            + String.valueOf(barco.getAnioFabricacion()) + " \t\t" + barco.getCantidadTripulantes());

}