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

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

Introduction

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

Prototype

@Override
    public <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)
            throws RestClientException 

Source Link

Usage

From source file:cn.org.once.cstack.utils.RestUtils.java

@SuppressWarnings("rawtypes")
public static String sendPostCommand(String url, String volume) {

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Accept", "text/plain");
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("Binds", volume);
    @SuppressWarnings("unchecked")
    HttpEntity request = new HttpEntity(params, headers);
    ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
    return response.toString();
}

From source file:main.ScorePipeline.java

/**
 * Send an event to the google analytics server for tool start monitoring.
 *//*w  w w  .j  a va 2 s  .co  m*/
private static void sendAnalyticsEvent() {
    String COLLECT_URL = "http://www.google-analytics.com/collect";
    String POST = "v=1&tid=UA-36198780-13&cid=35119a79-1a05-49d7-b876-bb88420f825b&uid=asuueffeqqss&t=event&ec=usage&ea=toolstart&el=spectrumsimilarity";

    //spring rest template
    RestTemplate restTemplate = new RestTemplate();
    HttpEntity<String> request = new HttpEntity<>(POST);
    ResponseEntity<String> postForEntity = restTemplate.postForEntity(COLLECT_URL, request, String.class);

    if (postForEntity.getStatusCode().equals(HttpStatus.OK)) {
        LOGGER.info("Successfully sent analytics event.");
    }
}

From source file:org.trustedanalytics.scoringengine.ATKScoringEngine.java

public Boolean score(float[] data) {
    String commaSeparatedNumbers = convertToCommaSeparated(data);
    String url = getUrl() + commaSeparatedNumbers;
    Float result;//from  w  ww.j ava 2  s  .c om
    try {
        RestTemplate template = new RestTemplate();
        ResponseEntity<String> response = template.postForEntity(url, null, String.class);
        String body = response.getBody();
        result = Float.parseFloat(body);
        LOG.debug("Score from scoring engine: {}", result);
    } catch (Exception ex) {
        LOG.warn("problem with getting scoring result! " + ex.getMessage(), ex);
        result = -100f;
    }

    return result.compareTo(1.0f) == 0;
}

From source file:de.codecentric.boot.admin.services.SpringBootAdminRegistratorTest.java

@Test
public void register_failed() {
    AdminProperties adminProps = new AdminProperties();
    adminProps.setUrl("http://sba:8080");
    AdminClientProperties clientProps = new AdminClientProperties();
    clientProps.setUrl("http://localhost:8080");
    clientProps.setName("AppName");

    RestTemplate restTemplate = mock(RestTemplate.class);
    when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class)))
            .thenThrow(new RestClientException("Error"));

    SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps,
            clientProps);/*  w w w  .  j a v a  2  s .  c o m*/
    boolean result = registrator.register();

    assertFalse(result);
}

From source file:de.codecentric.boot.admin.services.SpringBootAdminRegistratorTest.java

@Test
public void register_failed_conflict() {
    AdminProperties adminProps = new AdminProperties();
    adminProps.setUrl("http://sba:8080");
    AdminClientProperties clientProps = new AdminClientProperties();
    clientProps.setUrl("http://localhost:8080");
    clientProps.setName("AppName");

    RestTemplate restTemplate = mock(RestTemplate.class);
    when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class)))
            .thenReturn(new ResponseEntity<Application>(HttpStatus.CONFLICT));

    SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps,
            clientProps);//from w ww . jav  a2s  .  c om
    boolean result = registrator.register();

    assertFalse(result);
}

From source file:de.codecentric.boot.admin.services.SpringBootAdminRegistratorTest.java

@Test
public void register_successful() {
    AdminProperties adminProps = new AdminProperties();
    adminProps.setUrl("http://sba:8080");

    AdminClientProperties clientProps = new AdminClientProperties();
    clientProps.setUrl("http://localhost:8080");
    clientProps.setName("AppName");

    RestTemplate restTemplate = mock(RestTemplate.class);
    when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class)))
            .thenReturn(new ResponseEntity<Application>(HttpStatus.CREATED));

    SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps,
            clientProps);//from   w  w  w .j a  v a2  s  . c  o m
    boolean result = registrator.register();

    assertTrue(result);
    verify(restTemplate).postForEntity("http://sba:8080/api/applications",
            new Application("http://localhost:8080", "AppName"), Application.class);
}

From source file:com.appranix.adapter.service.AdapterServiceImpl.java

@Override
public int adapterCreate() throws Exception {
    try {// w w  w  .j  av  a 2s . com
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode node = mapper.createObjectNode();
        node.put("nsPath", "/");
        node.put("ciClassName", "Component");
        node.put("ciName", "test");

        final String uri = "http://localhost:9090/adapter/rest/cm/simple/cis";

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> result = restTemplate.postForEntity(uri, node, String.class);

        Map<String, Integer> map = mapper.readValue(result.getBody(), Map.class);

        Integer id = map.get("ciId");

        return id;
    } catch (Exception e) {
        throw new Exception("Error in Write Operation");
    }
}

From source file:net.orpiske.tcs.service.rest.functional.DomainCreateTest.java

@Test
public void testDomainCreation() {
    HttpEntity<Domain> requestEntity = new HttpEntity<Domain>(RestDataFixtures.customCsp(),
            getHeaders(USERNAME + ":" + GOOD_PASSWORD));

    RestTemplate template = new RestTemplate();
    ResponseEntity<Domain> responseEntity = template
            .postForEntity("http://localhost:8080/tcs/domain/terra.com.br", requestEntity, Domain.class);

    String path = responseEntity.getHeaders().getLocation().getPath();

    assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
    assertTrue("The returned path (" + path + ") does not match the expected one",
            path.startsWith("/tcs/domain/terra.com.br"));
}

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

@Override
public long generatePollId(List<Game> games, BGLTable table) {
    StrawPoll poll = generateRequestBodyPoll(games, generatePollName(table));
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<StrawPoll> request = new HttpEntity<>(poll, headers);
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    RestTemplate template = new RestTemplate(factory);
    ResponseEntity<StrawPoll> generatedPoll;
    generatedPoll = template.postForEntity(STRAW_POLL_URL, request, StrawPoll.class);

    return generatedPoll.getBody().getId();
}

From source file:net.orpiske.tcs.service.rest.functional.DomainCreateTest.java

@Test
public void testAuthenticationError() {
    HttpEntity<Domain> requestEntity = new HttpEntity<Domain>(RestDataFixtures.customCsp(),
            getHeaders(USERNAME + ":" + BAD_PASSWORD));

    RestTemplate template = new RestTemplate();

    try {/*ww w  . j a  va  2 s  .  c o m*/
        ResponseEntity<Domain> responseEntity = template
                .postForEntity("http://localhost:8080/tcs/domain/terra.com.br", requestEntity, Domain.class);

        fail("Request Passed incorrectly with status " + responseEntity.getStatusCode());
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}