[Spring] RestTemplate

Spring 3.0부터는 REST client를 쉽게 구현케 할 수 있는 RestTemplate이 도입되었다.

Main Method

HTTP method RestTemplate methods
DELETE delete(java.lang.String, java.lang.Object...)
GET getForObject(java.lang.String, java.lang.Class, java.lang.Object...)
getForEntity(java.lang.String, java.lang.Class, java.lang.Object...)
HEAD headForHeaders(java.lang.String, java.lang.Object...)
OPTIONS optionsForAllow(java.lang.String, java.lang.Object...)
POST postForLocation(java.lang.String, java.lang.Object, java.lang.Object...)
postForObject(java.lang.String, java.lang.Object, java.lang.Class, java.lang.Object...)
PUT put(java.lang.String, java.lang.Object, java.lang.Object...)
any exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, java.lang.Class, java.lang.Object...)
execute(java.lang.String, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor, java.lang.Object...)

Get Method

String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", 
String.class,"42", "21");

Post Method

String postUrl = url + “/login”;
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add(“username”, user);
map.add(“password”, password);
String result = restTemplate.postForObject(postUrl, map, String.class);

Post Method – how to  modify header and body

String reqUrl = Url + “/search”;

HttpHeaders headers = new HttpHeaders();
headers.add(“Authorization”, sessionKey);

MultiValueMap<String, String> postParams = new LinkedMultiValueMap<String, String>();
postParams.add(“search”, searchKey);  //body 조작

HttpEntity<MultiValueMap<String, String>> requestEntity
= new HttpEntity<MultiValueMap<String, String>>(postParams, headers);

result = restTemplate.exchange(reqUrl , HttpMethod.POST, requestEntity,    String.class).getBody();

Async Post

using execute – implement third parameter- RequestCallback, 4th – ResponseExtractor

then, called ResponseExtractor’s extractData method asynclonously.

//RequestCallback – doWithRequest  implemementation

static class RestRequestCallBack implements RequestCallback {

private String sessionKey;
private String searchKey;

private RestRequestCallBack(String sessionKey, String searchKey){
this.sessionKey = sessionKey;
this.searchKey = searchKey;

}
public void doWithRequest(ClientHttpRequest clientHttpRequest) throws IOException {
HttpHeaders request = clientHttpRequest.getHeaders();

request.add(“Authorization”, sessionKey);

request.add( “Accept”, “application/json” );

String search = “search=”+ searchKey;
byte searchbyte[] = search.getBytes();
clientHttpRequest.getBody().write(searchbyte, 0, searchbyte.length);
}
}
//ResponseExtractor -extractData  implemementation
private static class FileResponseExtractor implements ResponseExtractor<Object>
{
private final File file;
private       File file () { return this.file; }

private FileResponseExtractor ( File file )
{
this.file = file;
}

@Override
public Object extractData ( ClientHttpResponse response ) throws IOException
{
InputStream  is = response.getBody();
OutputStream os = new BufferedOutputStream( new FileOutputStream( file()));

IOUtils.copyLarge( is, os );
IOUtils.closeQuietly( is );
IOUtils.closeQuietly( os );

return null;
}
}

public String searchAsync(String sessionKey, String searchKey) {

Object result = null;

String reqUrl = Url + “/search”;

String downloadDir = “D:\\”;
String fileName = “mysearch.log”;
try {
result = restTemplate.execute(reqUrl,
HttpMethod.POST,
new RestRequestCallBack(sessionKey, searchKey),
new FileResponseExtractor( new File( downloadDir, fileName )));

} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();

}

return null;
}

—— execute test code async code——-
searchKey = “http 80”;
sessionKey = client.getSession(user, password);
client.searchAsync(sessionKey, searchKey);

HTTP method RestTemplate methods
DELETE delete(java.lang.String, java.lang.Object...)
GET getForObject(java.lang.String, java.lang.Class, java.lang.Object...)
getForEntity(java.lang.String, java.lang.Class, java.lang.Object...)
HEAD headForHeaders(java.lang.String, java.lang.Object...)
OPTIONS optionsForAllow(java.lang.String, java.lang.Object...)
POST postForLocation(java.lang.String, java.lang.Object, java.lang.Object...)
postForObject(java.lang.String, java.lang.Object, java.lang.Class, java.lang.Object...)
PUT put(java.lang.String, java.lang.Object, java.lang.Object...)
any exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, java.lang.Class, java.lang.Object...)
execute(java.lang.String, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor, java.lang.Object...)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s