Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- record Bar(int id);
- interface Foo {
- // returns some domain entity
- // if it doesnt exist return null
- // if it cannot be obtained due to system error throw runtime exception
- Bar getBarById(int id);
- }
- class ConcreteFoo implements Foo {
- public Bar getBarById(int id) {
- return new Bar(id);
- }
- }
- class HttpClientFoo implements Foo {
- private HttpClient client;
- public HttpClientFoo(HttpClient client) {
- this.client = client;
- }
- public Bar getBarById(int id) {
- // suppose client is raw http client that returns string
- try {
- Response<String> externalResult = this.client.get("/api/foo/" + id);
- if(externalResult.isSuccessful()) {
- String body = externalResult.body();
- // suppose some parsing method exists and returns Bar
- return parseBody(body);
- }
- return null;
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
- class ControllerFoo implements Foo {
- private Foo delegate;
- public ControllerFoo(Foo delegate) {
- this.delegate = delegate;
- }
- @GET
- @Path("/api/foo/{id}")
- public Bar getBarById(@PathParam("id") int id) {
- return delegate.getBarById(id);
- }
- }
- class Main {
- public static void main(String args[]) {
- Foo concreteFoo = new ConcreteFoo();
- Foo controller = new ControllerFoo(concreteFoo);
- Server.listen(3000)
- .controler(controler) // suppose theres a server utility class to host servers
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement