-
-
Save dfparker2002/cfdf04389807da39ec2bc5c2d400efa8 to your computer and use it in GitHub Desktop.
Spring RESTTEMPLATE example using post and GET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import java.util.HashMap; | |
import org.springframework.web.client.RestTemplate; | |
public class HelloWorld { | |
public static void main(String args[]) { | |
RestTemplate restTemplate = new RestTemplate(); | |
testGET(restTemplate); | |
testPOST(restTemplate); | |
} | |
private static void testPOST(RestTemplate restTemplate) { | |
HashMap<String, String> params = new HashMap<String, String>(); | |
params.put("username", "administrator"); | |
String response = restTemplate.postForObject("http://localhost:5000/ihub/v1/login", params, String.class,params); | |
System.out.println(response); | |
} | |
private static void testGET(RestTemplate restTemplate) { | |
Page page = restTemplate.getForObject("http://graph.facebook.com/gopivotal", Page.class); | |
System.out.println("Name: " + page.getName()); | |
System.out.println("About: " + page.getAbout()); | |
System.out.println("Phone: " + page.getPhone()); | |
System.out.println("Website: " + page.getWebsite()); | |
} | |
} | |
//corresponding pom.xml | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.actuate.rest</groupId> | |
<artifactId>com.actuate.rest</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>com.actuate.rest</name> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.0.2.RELEASE</version> | |
</parent> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
</dependency> | |
</dependencies> | |
<repositories> | |
<repository> | |
<id>spring-snapshots</id> | |
<url>http://repo.spring.io/libs-snapshot</url> | |
<snapshots><enabled>true</enabled></snapshots> | |
</repository> | |
</repositories> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment