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:cz.muni.fi.mushroomhunter.restclient.AllLocationSwingWorker.java

@Override
protected List<LocationDto> doInBackground() throws Exception {
    String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    HttpEntity<String> request = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<LocationDto[]> responseEntity = restTemplate.exchange(
            RestClient.SERVER_URL + "pa165/rest/location/", HttpMethod.GET, request, LocationDto[].class);
    LocationDto[] locationDtoArray = responseEntity.getBody();
    List<LocationDto> locationDtoList = new ArrayList<>();
    locationDtoList.addAll(Arrays.asList(locationDtoArray));
    return locationDtoList;
}

From source file:sample.undertow.SampleUndertowApplicationTests.java

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    RestTemplate restTemplate = new TestRestTemplate();
    ResponseEntity<byte[]> entity = restTemplate.exchange("http://localhost:" + this.port, HttpMethod.GET,
            requestEntity, byte[].class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {/*from   w w  w . j a va  2 s  . c o  m*/
        assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}

From source file:org.echocat.marquardt.example.ServiceLoginIntegrationTest.java

private void whenAccessingUnprotectedResourceOnService() {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.exchange(baseUriOfApp() + "/exampleservice/someUnprotectedResource", HttpMethod.POST, null,
            Void.class);
}

From source file:cz.muni.fi.mushroomhunter.restclient.AllMushroomsSwingWorker.java

@Override
protected List<MushroomDto> doInBackground() throws Exception {
    String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    HttpEntity<String> request = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<MushroomDto[]> responseEntity = restTemplate.exchange(
            RestClient.SERVER_URL + "pa165/rest/mushroom/", HttpMethod.GET, request, MushroomDto[].class);
    MushroomDto[] mushroomDtoArray = responseEntity.getBody();
    List<MushroomDto> mushroomDtoList = new ArrayList<>();
    mushroomDtoList.addAll(Arrays.asList(mushroomDtoArray));
    return mushroomDtoList;
}

From source file:HCEngine.java

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
    ResponseEntity<String> stringResponseEntity = null;
    try (CloseableHttpClient hc = createCloseableHttpClient()) {
        for (int i = 0; i < requestOptions.getCount(); i++) {
            final HttpHeaders headers = new HttpHeaders();
            for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) {
                headers.put(e.getKey(), Collections.singletonList(e.getValue()));
            }/*  w  ww.  j  a v  a  2 s .  c  o  m*/

            final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

            RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory(hc));

            stringResponseEntity = template.exchange(requestOptions.getUrl(), HttpMethod.GET, requestEntity,
                    String.class);
            System.out.println(stringResponseEntity.getBody());

        }
        return stringResponseEntity;
    }
}

From source file:ca.qhrtech.services.implementations.BGGServiceImpl.java

/**
 * Consumes a BGG XML API End Point and converts the response to a usable
 * POJO using the passed converter/*from   w w w  .j a v  a2  s  . c  om*/
 *
 * @param <T> - Class the response should expect
 * @param <K> - Class the converter produces
 * @param uri - end point
 * @param t - Class the response should expect
 * @param converter - converts T to K
 * @return - results of the conversion
 */
private <T, K> K consumeBggApi(URI uri, Class<T> t, Converter<T, K> converter) {
    RestTemplate template = new RestTemplate();
    ResponseEntity<T> response;
    response = template.exchange(uri, HttpMethod.GET, buildXmlHttpHeader(), t);
    return converter.convert(response.getBody());
}

From source file:org.sitenv.referenceccda.services.VocabularyService.java

public Map<String, Map<String, List<String>>> getMapOfSenderAndRecieverValidationObjectivesWithReferenceFiles() {
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<GithubResponseWrapper> responseEntity = restTemplate.exchange(GITHUB_URL, HttpMethod.GET,
            null, new ParameterizedTypeReference<GithubResponseWrapper>() {
            });// www  .  j  av  a 2s.  c o  m

    Map<String, Map<String, List<String>>> messageTypeValidationObjectiveReferenceFilesMap = new HashMap<>();
    for (TestDataTreeWrapper testDataTreeWrapper : responseEntity.getBody().getTree()) {
        if (!(testDataTreeWrapper.getPath().equalsIgnoreCase("license")
                || testDataTreeWrapper.getPath().equalsIgnoreCase("README.md"))) {
            if (isMessageTypeInMap(messageTypeValidationObjectiveReferenceFilesMap, testDataTreeWrapper)) {
                if (isValidationObjectiveInMap(messageTypeValidationObjectiveReferenceFilesMap,
                        testDataTreeWrapper)) {
                    addReferenceFileNameToListInValidationObjectiveMap(
                            messageTypeValidationObjectiveReferenceFilesMap, testDataTreeWrapper);
                } else {
                    addValidationObjectiveToMap(messageTypeValidationObjectiveReferenceFilesMap,
                            testDataTreeWrapper);
                }
            } else {
                addMessageTypeToMap(messageTypeValidationObjectiveReferenceFilesMap, testDataTreeWrapper);
            }
        }
    }
    return messageTypeValidationObjectiveReferenceFilesMap;
}

From source file:io.fabric8.che.starter.client.WorkspaceClient.java

public List<Workspace> listWorkspaces(String cheServerUrl) {
    String url = CheRestEndpoints.LIST_WORKSPACES.generateUrl(cheServerUrl);
    RestTemplate template = new RestTemplate();
    ResponseEntity<List<Workspace>> response = template.exchange(url, HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Workspace>>() {
            });// w w  w  . j av a 2s .  c  o  m

    return response.getBody();
}

From source file:org.androidannotations.test15.rest.HttpMethodServiceTest.java

@Test
@SuppressWarnings("unchecked")
public void useHeadHttpMethod() {
    HttpMethodsService_ service = new HttpMethodsService_(null);

    RestTemplate restTemplate = mock(RestTemplate.class);
    ResponseEntity<Object> response = mock(ResponseEntity.class);
    when(restTemplate.exchange(Matchers.anyString(), Matchers.<HttpMethod>eq(HttpMethod.HEAD),
            Matchers.<HttpEntity<?>>any(), Matchers.<Class<Object>>any())).thenReturn(response);

    service.setRestTemplate(restTemplate);

    service.head();/*from  w  w  w.  ja va2  s  .  c  om*/

    verify(restTemplate).exchange(Matchers.anyString(), Matchers.<HttpMethod>eq(HttpMethod.HEAD),
            Matchers.<HttpEntity<?>>any(), Matchers.<Class<Object>>any());
}

From source file:org.androidannotations.test15.rest.HttpMethodServiceTest.java

@Test
@SuppressWarnings("unchecked")
public void useOptionsHttpMethod() {
    HttpMethodsService_ service = new HttpMethodsService_(null);

    RestTemplate restTemplate = mock(RestTemplate.class);
    ResponseEntity<Object> response = mock(ResponseEntity.class);
    when(restTemplate.exchange(Matchers.anyString(), Matchers.<HttpMethod>eq(HttpMethod.OPTIONS),
            Matchers.<HttpEntity<?>>any(), Matchers.<Class<Object>>any())).thenReturn(response);
    HttpHeaders headers = mock(HttpHeaders.class);
    when(response.getHeaders()).thenReturn(headers);

    service.setRestTemplate(restTemplate);

    service.options();/*w  w w. j  av  a2 s.c  o  m*/

    verify(restTemplate).exchange(Matchers.anyString(), Matchers.<HttpMethod>eq(HttpMethod.OPTIONS),
            Matchers.<HttpEntity<?>>any(), Matchers.<Class<Object>>any());
}