I had simple Dropwizard rest service application which had only two layers: controller layer (aka resource) and persistence layer (aka dao). Dropwizard application was small and concise and worked smoothly.
But then, after while some requirements changed and I need to add some business logic for processing incoming json object and later persisting it into database. So, I am looking best approach for adding this service layer between controller and persistence layers.
Could you please advice best method for adding service layer in application. Thanks.
The controller (aka resource):
@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);
}
}
The service layer:
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);
}
}
Application class:
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);
}
}
I can launch application successfully but when it comes to processing request it fails 404 error code. And in log see this message:
org.glassfish.jersey.internal.inject.Providers: A provider com.laboratory.dropwizard.service.PersonService registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.laboratory.dropwizard.service.PersonService will be ignored.
What is wrong with Dropwizard and how can I introduce working service layer? Thanks.