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

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

Introduction

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

Prototype

@Override
    public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity,
            ParameterizedTypeReference<T> responseType) throws RestClientException 

Source Link

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  w ww .  jav  a  2s  .c o  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.// w w w  . j  a  va2s .co  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:io.fabric8.che.starter.client.StackClient.java

public List<Stack> listStacks(String cheServerUrl) {
    String url = CheRestEndpoints.LIST_STACKS.generateUrl(cheServerUrl);
    RestTemplate template = new RestTemplate();
    ResponseEntity<List<Stack>> response = template.exchange(url, HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Stack>>() {
            });/*  ww  w  . ja va 2 s. co  m*/
    return response.getBody();
}

From source file:com.opensearchserver.hadse.cluster.ClusterTest.java

@Test
public void t01_get() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    RestTemplate template = new RestTemplate();

    ResponseEntity<NodeItem[]> entity = template.exchange("http://localhost:8080/_cluster", HttpMethod.GET,
            null, NodeItem[].class);
    assertTrue(entity.getBody().length >= 1);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
}

From source file:com.vinidsl.googleioextended.rest.BaseGet.java

@Override
public T performRequest() {
    HttpEntity<T> requestEntity = new HttpEntity<>(getHeaders());
    RestTemplate restTemplate = getRestTemplate();
    ResponseEntity<T> requestResult = restTemplate.exchange(getUrl(), HttpMethod.GET, requestEntity,
            getResponseClass());/* w w  w.j  a  v a2s  . c o  m*/
    T result = requestResult.getBody();
    return result;
}

From source file:io.github.cdelmas.spike.springboot.hello.SampleController.java

@RequestMapping("/")
@ResponseBody/*  w  w  w .  j  a va  2s .  c  o m*/
String home(@RequestHeader("Authorization") String authToken) {

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("Accept", "application/json");
    headers.add("Authorization", authToken);
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(null, headers);

    RestTemplate rest = new RestTemplate();
    ResponseEntity<Car[]> responseEntity = rest.exchange("https://localhost:8443/cars", HttpMethod.GET,
            requestEntity, Car[].class);
    List<Car> cars = asList(responseEntity.getBody());

    return "Hello World! " + cars.stream().map(Car::getName).collect(toList());
}

From source file:com.opensearchserver.hadse.index.IndexTest.java

@Test
public void t02_createDefaultIndex() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    RestTemplate template = new RestTemplate();

    ResponseEntity<String> entity = template.exchange("http://localhost:8080/my_index", HttpMethod.PUT, null,
            String.class);

    assertEquals(HttpStatus.CREATED, entity.getStatusCode());
}

From source file:com.opensearchserver.hadse.index.IndexTest.java

@Test
public void t03_exists() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    RestTemplate template = new RestTemplate();

    ResponseEntity<String> entity = template.exchange("http://localhost:8080/my_index", HttpMethod.GET, null,
            String.class);

    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
}

From source file:com.unknownpkg.StupsSwaggerCodegenIT.java

@Test
public void getResources() {
    RestTemplate rest = new RestTemplate();
    ResponseEntity<List<SwaggerResource>> responseEntity = rest.exchange(
            "http://localhost:" + port + "/swagger-resources", HttpMethod.GET, null,
            swaggerResourceParameterizedType);

    List<SwaggerResource> swaggerResourceList = responseEntity.getBody();
    Assertions.assertThat(swaggerResourceList).isNotEmpty();

    SwaggerResource resource = swaggerResourceList.get(0);
    Assertions.assertThat(resource).isNotNull();
    Assertions.assertThat(resource.getSwaggerVersion()).isEqualTo("2.0");
    Assertions.assertThat(resource.getName()).isEqualTo("default");
    Assertions.assertThat(resource.getLocation()).isEqualTo("/v2/api-docs");
}

From source file:com.opensearchserver.hadse.index.IndexTest.java

@Test
public void t01_deleteIndex() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    RestTemplate template = new RestTemplate();

    ResponseEntity<String> entity = template.exchange("http://localhost:8080/my_index", HttpMethod.DELETE, null,
            String.class);
    assertNotNull(entity);/*  w  ww . j  a  v  a  2 s  .  co  m*/
    HttpStatus res = entity.getStatusCode();
    assertNotNull(res);
    switch (res) {
    case OK:
        assertEquals(HttpStatus.OK, res);
        break;
    case NOT_FOUND:
        assertEquals(HttpStatus.NOT_FOUND, res);
        break;
    default:
        fail("Unexpected result: " + res);
        break;
    }
}