【问题标题】:Dropwizard multi-layer applicationDropwizard 多层应用
【发布时间】:2018-04-10 13:36:48
【问题描述】:

我有一个简单的 Dropwizard 休息服务应用程序,它只有两层:控制器层(又名资源)和 持久层(又名道)。 Dropwizard 应用程序小巧简洁,运行流畅。

但是,在一些需求发生变化之后,我需要添加一些业务逻辑来处理传入的 json 对象,然后将其持久化到数据库中。因此,我正在寻找在控制器和持久层之间添加此 服务层 的最佳方法。

您能否建议在应用程序中添加服务层的最佳方法。谢谢。

控制器(又名资源):

@Path("/person")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class PersonResource {

   private PersonService personService;

   public PersonResource(PersonService personService) {
        this.personService = personService;
    }


   @GET
    public List<Person> getAll(){
        return personService.getAll();
    }

   @GET
    @Path("/{id}")
    public Person findById(@PathParam("id") Integer id){
        return personService.findPersonById(id);
    }
}

服务层:

public class PersonService {

   private PersonDAO personDAO;

   public PersonService(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }

   @UnitOfWork
    public List<Person> getAll(){
        return personDAO.getAll();
    }

   @UnitOfWork
    public Person findPersonById(Integer id){
        return personDAO.findById(id);
    }
}

应用类:

public class DemoApplication extends Application<DemoConfiguration> {

   public static void main(final String[] args) throws Exception {
        new DemoApplication().run(args);
    }

   private final HibernateBundle<DemoConfiguration> hibernateBundle = new HibernateBundle<DemoConfiguration>(Person.class) {
        @Override
        public DataSourceFactory getDataSourceFactory(DemoConfiguration configuration) {
            return configuration.getDatabaseAppDataSourceFactory();
        }
    };

   @Override
    public void initialize(final Bootstrap<DemoConfiguration> bootstrap) {
        bootstrap.addBundle(hibernateBundle);
    }

   @Override
    public void run(final DemoConfiguration configuration, final Environment environment) {
        final PersonDAO personDAO = new PersonDAO(hibernateBundle.getSessionFactory());
        final PersonService personResource = new PersonService(personDAO);
        environment.jersey().register(personResource);
    }
}

我可以成功启动应用程序,但是在处理请求时它会失败 404 错误代码。并在日志中看到此消息:

org.glassfish.jersey.internal.inject.Providers:在 SERVER 运行时注册的提供程序 com.laboratory.dropwizard.service.PersonService 不实现任何适用于 SERVER 运行时的提供程序接口。由于约束 提供者 com.laboratory.dropwizard.service.PersonService 的配置问题将被忽略。

Dropwizard 有什么问题,如何引入工作服务层? 谢谢。

【问题讨论】:

  • 你能解决这个问题吗?

标签: rest dependency-injection dropwizard multi-layer


【解决方案1】:

您收到错误(警告)是因为您尝试register PersonServiceregister 方法仅用于注册资源和已知的提供程序类型。您可能想要做的是使用依赖注入。您要做的是将 DAO 和服务绑定到注入框架。然后你可以使用@Inject 在需要的地方注入它们。

@Override
public void run(DemoConfiguration conf, Environment env) {
    env.jersey().register(new AbstractBinder() {
        @Override
        public void configure() {
            bindAsContract(PersonDAO.class).in(Singleton.class);
            bindAsContract(PersonService.class).in(Singleton.class);
        }
    });
}

现在您可以将 DAO 注入到服务中,并将服务注入到资源中

public class PersonService {
    private PersonDAO dao;

    @Inject
    public PersonService(PersonDAO dao){
        this.dao = dao;
    }
}

@Path("people")
public class PersonResource {
    private PersonService service;

    @Inject
    public PersonResource(PersonService service) {
        this.service = service;
    }
}

【讨论】:

  • 资源中的服务始终为空。我的资源中还有一个构造函数,如何解决这个问题,请您帮忙
  • @Shubh 如果您需要帮助,请发布另一个包含所有详细信息的问题。
【解决方案2】:

From the documentation:

当前使用 @UnitOfWork 注释创建事务仅适用于 Jersey 管理的资源。如果您想在泽西岛资源之外使用它,例如在身份验证器中,您应该使用 UnitOfWorkAwareProxyFactory 实例化您的类。

说明这一点的代码示例是:

SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory());
ExampleAuthenticator exampleAuthenticator = new UnitOfWorkAwareProxyFactory(hibernateBundle)
               .create(ExampleAuthenticator.class, SessionDao.class, dao);

沿着这条路线尝试一些东西。

【讨论】:

  • 我从文档中看到了这个例子,但我想实现的不是事务管理,其实我并不关心@UnitOfWork。我想了解如何使用通常的依赖注入在 Dropwizard 中构建多层应用程序。三层:Controller - Service - DAO。控制器直接调用 dao 的例子很多。看起来environment.jersey().register() 只允许注册带有 dao 的控制器作为构造函数参数。如果我需要使用服务的控制器怎么办?正确注册的方法是什么? @Lutz Horn
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 2019-10-23
  • 2018-03-15
  • 2013-01-05
  • 1970-01-01
  • 2014-11-27
相关资源
最近更新 更多