Example usage for org.springframework.http HttpEntity HttpEntity

List of usage examples for org.springframework.http HttpEntity HttpEntity

Introduction

In this page you can find the example usage for org.springframework.http HttpEntity HttpEntity.

Prototype

public HttpEntity(MultiValueMap<String, String> headers) 

Source Link

Document

Create a new HttpEntity with the given headers and no body.

Usage

From source file:com.joseph.california.test.restapi.PersonRestControllerTest.java

private HttpEntity<?> getHttpEntity() {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    return requestEntity;
}

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()));
            }//www.  j a v  a2  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:id.co.teleanjar.ppobws.restclient.RestClientService.java

public User getUser(String username) {
    RestTemplate restTemplate = new RestTemplate();

    //User user = restTemplate.getForObject("http://localhost:8080/ppobws/api/user?username=" + username, User.class);

    String uri = "http://localhost:8080/ppobws/api/user?username=" + username;

    ResponseEntity<User> respEntity = restTemplate.exchange(uri, HttpMethod.GET,
            new HttpEntity<String>(createHeaders("superuser", "passwordku")), User.class);

    return respEntity.getBody();
}

From source file:org.mimacom.sample.integration.patterns.user.service.integration.AsyncSearchServiceIntegration.java

public void indexUser(User user, Consumer<Void> successConsumer, Consumer<Throwable> failureConsumer) {
    LOG.info("going to send request to index user '{}' '{}'", user.getFirstName(), user.getLastName());

    HttpEntity<User> requestEntity = new HttpEntity<>(user);
    ListenableFuture<ResponseEntity<String>> listenableFuture = this.asyncRestTemplate
            .postForEntity(this.searchServiceUrl + "/index", requestEntity, String.class);

    listenableFuture.addCallback(result -> {
        LOG.info("user '{}' '{}' was indexed and response status code was '{}'", user.getFirstName(),
                user.getLastName(), result.getStatusCode());
        successConsumer.accept(null);//  w w  w  .j av a  2 s  . co m
    }, failureConsumer::accept);

}

From source file:com.alcatel.hello.actuator.ui.SampleActuatorUIApplicationTests.java

@Test
public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port,
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);

    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(),
            entity.getBody().contains("<title>Hello"));
}

From source file:org.intermine.app.net.request.JsonPostRequest.java

protected String post(String uriString, Map<String, ?> params, String obj) {
    RestTemplate rtp = getRestTemplate();
    HttpHeaders headers = getHeaders();/*  www .jav  a 2s.co m*/

    HttpEntity<String> req;
    if (Strs.isNullOrEmpty(obj)) {
        req = new HttpEntity<>(headers);
    } else {
        req = new HttpEntity<>(obj, headers);
    }

    ResponseEntity<String> res;

    String uri = Uris.expandQuery(uriString, params);

    res = rtp.exchange(uri, POST, req, String.class);

    return res.getBody();
}

From source file:guru.nidi.ramltester.UriTest.java

@RequestMapping(value = { "/raml/v1/{def}/{type}", "/v1/{def}/{type}", "/{def}/{type}",
        "/sub-raml/{a}/{b}/{c}/{d}" })
@ResponseBody/*from   www  . j  ava 2s  .c o  m*/
public HttpEntity<String> test() {
    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_HTML);
    return new HttpEntity<>(headers);
}

From source file:fleetmgt.ApiControllerTest.java

@Test
public void shouldReturnFleetCalculation() throws Exception {
    //given//from w ww .  ja va 2  s.  c  om
    final FleetConstraints fleetConstraints = new FleetConstraints(new int[] { 15, 10 }, 12, 5);

    // when:
    final ResponseEntity<Map> response = testRestTemplate.exchange("/fleet-calculation", HttpMethod.POST,
            new HttpEntity<>(fleetConstraints), Map.class);

    // then:
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(response.getBody()).isNotNull();
    assertThat(response.getBody()).containsKey("fleet_engineers");
    assertThat(response.getBody()).containsKey("fleet_manager_location");
}

From source file:HCNIOEngine.java

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
    ResponseEntity<String> stringResponseEntity = null;
    try (CloseableHttpAsyncClient hc = createCloseableHttpAsyncClient()) {
        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()));
            }/*from   ww  w  . ja  v  a2s . c  om*/

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

            AsyncRestTemplate template = new AsyncRestTemplate(
                    new HttpComponentsAsyncClientHttpRequestFactory(hc));
            final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(),
                    HttpMethod.GET, requestEntity, String.class);
            stringResponseEntity = exchange.get();
            System.out.println(stringResponseEntity.getBody());

        }
        return stringResponseEntity;
    }
}

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

@Override
protected Integer doInBackground() throws Exception {
    int selectedRow = restClient.getTblMushroom().getSelectedRow();
    RestTemplate restTemplate = new RestTemplate();

    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.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {
        @Override// w  w w  .  j  ava2  s .  c  o  m
        protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
            if (HttpMethod.DELETE == httpMethod) {
                return new HttpEntityEnclosingDeleteRequest(uri);
            }
            return super.createHttpUriRequest(httpMethod, uri);
        }
    });

    restTemplate.exchange(
            RestClient.SERVER_URL + "pa165/rest/mushroom/" + RestClient.getMushroomIDs().get(selectedRow),
            HttpMethod.DELETE, request, MushroomDto.class);

    RestClient.getMushroomIDs().remove(selectedRow);
    return selectedRow;
}