Created
January 21, 2014 08:43
-
-
Save making/8536413 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 demo.foo; | |
import demo.todo.Todo; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.core.ParameterizedTypeReference; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.util.concurrent.ListenableFuture; | |
import org.springframework.util.concurrent.ListenableFutureCallback; | |
import org.springframework.web.client.AsyncRestTemplate; | |
import javax.inject.Inject; | |
import javax.inject.Named; | |
import java.util.List; | |
/** | |
* Created by makits on 14/01/21. | |
*/ | |
@Named | |
public class AsyncSample implements CommandLineRunner { | |
Logger logger = LoggerFactory.getLogger(AsyncSample.class); | |
@Inject | |
AsyncRestTemplate asyncRestTemplate; | |
@Override | |
public void run(String... args) throws Exception { | |
System.out.println("foo"); | |
{ | |
String url = "http://spring-boot-demo.herokuapp.com/api/todos"; | |
ListenableFuture<ResponseEntity<List<Todo>>> result = asyncRestTemplate | |
.exchange(url, HttpMethod.GET, null/*header*/, | |
new ParameterizedTypeReference<List<Todo>>() { | |
}); | |
System.out.println(result.get()); | |
} | |
{ | |
String url = "http://spring-boot-demo.herokuapp.com/api/todos/{todoId}"; | |
ListenableFuture<ResponseEntity<Todo>> result = asyncRestTemplate.getForEntity(url, Todo.class, 1); | |
System.out.println(result.get()); | |
} | |
{ | |
String url = "http://spring-boot-demo.herokuapp.com/api/todos/{todoId}"; | |
ListenableFuture<ResponseEntity<Todo>> result = asyncRestTemplate.getForEntity(url, Todo.class, 1); | |
result.addCallback(new ListenableFutureCallback<ResponseEntity<Todo>>() { | |
@Override | |
public void onSuccess(ResponseEntity<Todo> result) { | |
logger.debug("result={}", result.getBody()); | |
} | |
@Override | |
public void onFailure(Throwable t) { | |
logger.error("error!", t); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment