2017-03-02 3 views
1

vertx.io에 대한 자습서를 여러 개 읽었지만 반복 코드를 최소화하는 방법을 여전히 이해할 수 없습니다.vertx에서 상용구 코드의 양을 줄이는 방법

예를 들어 DB에서 데이터를 가져 오는 RESTful 서비스를 구현해야합니다.

AdministratorService.java :

public void getAll(RoutingContext routingContext) { 
    jdbc.getConnection(ar -> { 
     SQLConnection connection = ar.result(); 
     connection.query(Queries.SELECT_ALL_ADMINS, result -> { 
     List<Administrator> admins = result.result().getRows().stream().map(Administrator::new).collect(Collectors.toList()); 
     routingContext.response() 
      .putHeader("content-type", "application/json; charset=utf-8") 
      .end(Json.encodePrettily(admins)); 
     connection.close(); 
     }); 
    }); 
    } 

    public void getOneById(RoutingContext routingContext) { 
    final String id = routingContext.request().getParam("id"); 
    if (id == null) { 
     routingContext.response().setStatusCode(400).end(); 
    } else { 
     jdbc.getConnection(ar -> { 
     // Read the request's content and create an instance of Administrator. 
     SQLConnection connection = ar.result(); 
     select(id, connection, Queries.SELECT_ONE_ADMIN_BY_ID, result -> { 
      if (result.succeeded()) { 
      routingContext.response() 
       .setStatusCode(200) 
       .putHeader("content-type", "application/json; charset=utf-8") 
       .end(Json.encodePrettily(result.result())); 
      } else { 
      routingContext.response() 
       .setStatusCode(404).end(); 
      } 
      connection.close(); 
     }); 
     }); 
    } 
    } 

CustomerService.java :

public void getAll(RoutingContext routingContext) { 
    jdbc.getConnection(ar -> { 
     SQLConnection connection = ar.result(); 
     connection.query(Queries.SELECT_ALL_CUSTOMERS, result -> { 
     List<Customer> customers = result.result().getRows().stream().map(Customer::new).collect(Collectors.toList()); 
     routingContext.response() 
      .putHeader("content-type", "application/json; charset=utf-8") 
      .end(Json.encodePrettily(customers)); 
     connection.close(); 
     }); 
    }); 
    } 


    public void getOneById(RoutingContext routingContext) { 
    final String id = routingContext.request().getParam("id"); 
    if (id == null) { 
     routingContext.response().setStatusCode(400).end(); 
    } else { 
     jdbc.getConnection(ar -> { 
     // Read the request's content and create an instance of Administrator. 
     SQLConnection connection = ar.result(); 
     select(id, connection, Queries.SELECT_ONE_CUSTOMER_BY_ID, result -> { 
      if (result.succeeded()) { 
      routingContext.response() 
       .setStatusCode(200) 
       .putHeader("content-type", "application/json; charset=utf-8") 
       .end(Json.encodePrettily(result.result())); 
      } else { 
      routingContext.response() 
       .setStatusCode(404).end(); 
      } 
      connection.close(); 
     }); 
     }); 
    } 
    } 

하지 나는 테이블 (고객, 관리자) 및 구현 서비스 클래스에 대한이 개 빈 클래스를 준비했습니다 그 부분을보기가 어렵다.

.routingContext.response() 
.putHeader("content-type", "application/json; charset=utf-8") 

이 각 방법에서 반복됩니다. 일반적으로 이러한 클래스 간의 차이점은 SQL 요청과 Bean 클래스입니다.

예제를 공유하거나 방법을 변경하는 방법을 보여줄 수 있습니까?

답변

3

VertX는 프레임 워크가 아니기 때문에 일부 개발자는 자신의 구조를 쉽게 디자인 할 수 있지만 일부는 악몽이됩니다. 당신이 찾고있는 것은 사전 설계된 프레임 워크이며 라우터, 컨트롤러, DB 연결이 준비되어 있습니다. 외관상으로는 vertx가 아니라, 라이브러리와 같아서 원하는대로 확장 할 수 있습니다.

모든 서비스 기능에 대해 SQL 연결을 얻는 코드를 볼 수 있습니다. Spring과 같은 다른 프레임 워크에서 작업 한 적이 있다면 DI를 사용하여 이미 연결을 사용할 수 있습니다. DI, 일부 MVC 디자인을 구현해야하며 상용구 코드가 제거됩니다.

비슷한 결과를 보였지만 MongoDB는 비슷한 결과를 보였습니다. 3.4 (이번 주에 발표되는) 것을

VertX with MongoDB

+0

주 (https://github.com/vert-x3/vertx-web 새로운 [콘텐츠 형식을 자동으로 설정하는 vertx - 웹 핸들러]있다/pull/524) – tsegismont