Created
August 9, 2012 19:13
-
-
Save dwelch2344/3307251 to your computer and use it in GitHub Desktop.
Spring RestTemplate w/ Basic Auth
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
<?xml version="1.0" encoding="UTF-8"?> | |
<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"> | |
<!-- Http Client --> | |
<dependency> | |
<groupId>org.apache.httpcomponents</groupId> | |
<artifactId>httpclient</artifactId> | |
<version>4.1</version> | |
</dependency> | |
</project> |
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
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.impl.client.BasicCredentialsProvider; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.log4j.ConsoleAppender; | |
import org.apache.log4j.Level; | |
import org.apache.log4j.PatternLayout; | |
import org.springframework.http.client.ClientHttpRequestFactory; | |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
import org.springframework.web.client.RestTemplate; | |
public class Example { | |
private final RestTemplate template; | |
public Stripe(String user, String password) { | |
DefaultHttpClient httpClient = new DefaultHttpClient(); | |
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | |
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password); | |
httpClient.setCredentialsProvider(credentialsProvider); | |
ClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(httpClient); | |
template = new RestTemplate(rf); | |
} | |
private <T> T getObject(String url, Class<T> klazz){ | |
String json = template.getForObject( url(url), String.class); | |
return null; // TODO deserialize json String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment