Ratpack HTTP Client
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.handler;
|
||||
|
||||
import com.baeldung.repository.EmployeeRepository;
|
||||
import com.baeldung.model.Employee;
|
||||
import ratpack.exec.Promise;
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
|
||||
public class EmployeeHandler implements Handler {
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
EmployeeRepository repository = ctx.get(EmployeeRepository.class);
|
||||
Long id = Long.valueOf(ctx.getPathTokens()
|
||||
.get("id"));
|
||||
Promise<Employee> employeePromise = repository.findEmployeeById(id);
|
||||
employeePromise.map(employee -> employee.getName())
|
||||
.then(name -> ctx.getResponse()
|
||||
.send(name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.handler;
|
||||
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
|
||||
public class FooHandler implements Handler {
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
ctx.getResponse()
|
||||
.send("Hello Foo!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.handler;
|
||||
|
||||
import ratpack.exec.Promise;
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.http.client.ReceivedResponse;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class RedirectHandler implements Handler {
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
HttpClient client = ctx.get(HttpClient.class);
|
||||
URI uri = URI.create("http://localhost:5050/employee/1");
|
||||
Promise<ReceivedResponse> responsePromise = client.get(uri);
|
||||
responsePromise.map(response -> response.getBody()
|
||||
.getText()
|
||||
.toUpperCase())
|
||||
.then(responseText -> ctx.getResponse()
|
||||
.send(responseText));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user