Java Code Examples for org.springframework.web.client.RestTemplate#postForLocation()

The following examples show how to use org.springframework.web.client.RestTemplate#postForLocation() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Spring-Boot-Book   File: PostController.java    License: Apache License 2.0 5 votes vote down vote up
@RequestMapping("/postForLocation")
public URI postForLocation() {
    // 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
    MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
    paramMap.add("name", "20180416");
    paramMap.add("id", 4);
    RestTemplate client = restTemplateBuilder.build();
    User user = new User();
    user.setName("hongwei");
    user.setId(6);
    URI response = client.postForLocation("http://localhost:8080/postuser", paramMap);
    return response;
}
 
Example 2
Source Project: Spring-Boot-Book   File: PostTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void postForLocation() {
    // 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
    MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
    paramMap.add("name", "longzhonghua");
    paramMap.add("id", 4);

    RestTemplate client = restTemplateBuilder.build();

    URI response = client.postForLocation("http://localhost:8080/post",paramMap);

     System.out.println(response);
}
 
Example 3
@Test
public void testSample() throws Exception {

	// Run Source app
	SpringApplicationBuilder sourceBuilder = new SpringApplicationBuilder(FunctionalSourceApplication.class)
			.resourceLoader(new PropertyRemovingResourceLoader("spring-cloud-gcp-pubsub-stream-binder-functional-sample-source"));
	sourceBuilder.run();


	//Run Sink app
	SpringApplicationBuilder sinkBuilder = new SpringApplicationBuilder(FunctionalSinkApplication.class)
		.resourceLoader(new PropertyRemovingResourceLoader("spring-cloud-gcp-pubsub-stream-binder-functional-sample-sink"));
	sinkBuilder.run();


	// Post message to Source over HTTP.
	MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
	String message = "test message " + UUID.randomUUID();
	map.add("messageBody", message);
	map.add("username", "integration-test-user");

	RestTemplate restTemplate = new RestTemplate();

	URI redirect = restTemplate.postForLocation("http://localhost:8080/postMessage", map);
	assertThat(redirect.toString()).isEqualTo("http://localhost:8080/index.html");

	Awaitility.await().atMost(10, TimeUnit.SECONDS)
			.until(() -> this.output.getOut()
					.contains("New message received from integration-test-user: " + message));

}
 
Example 4
@Test
public void test_postForLocation() throws URISyntaxException {
    RestTemplate restTemplate = new RestTemplate();
    URI location = restTemplate.postForLocation(baseUrl, null);

    isTrue(location.equals(new URI("http://example.com")));
}
 
Example 5
Source Project: expper   File: MailService.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void sendViaMailgunApi(EmailParams emailParams) {
    String apiUrl = jHipsterProperties.getMailgun().getApiUrl();
    String apiKey = jHipsterProperties.getMailgun().getApiKey();

    MultiValueMap<String, Object> vars = new LinkedMultiValueMap<>();
    vars.add("from", jHipsterProperties.getMailgun().getFrom());
    vars.add("to", emailParams.getTo());
    vars.add("subject", emailParams.getSubject());
    vars.add("html", emailParams.getContent());

    RestTemplate restTemplate = new BasicAuthRestTemplate(MAILGUN_USER_NAME, apiKey);
    restTemplate.postForLocation(apiUrl, vars);

    log.debug("Email sent successfully.");
}
 
Example 6
@Test // #72
void usesUtf8ToDecodePayload() {

	RestTemplate template = new RestTemplate();

	MultiValueMap<Object, Object> parameters = new LinkedMultiValueMap<>();
	parameters.add("value", "äöü€");

	template.postForLocation(String.format("http://localhost:%s/encoding", port), parameters);
}