├── .gitignore
├── README.md
├── configuration.yml
├── pom.xml
└── src
└── main
├── java
└── eu
│ └── kielczewski
│ └── example
│ ├── ExampleService.java
│ ├── config
│ ├── DatabaseConfiguration.java
│ ├── ExampleServiceConfiguration.java
│ └── MessagesConfiguration.java
│ ├── dao
│ ├── UserDao.java
│ └── UserDaoImpl.java
│ ├── model
│ └── User.java
│ └── resource
│ ├── HelloResource.java
│ └── UserResource.java
└── resources
└── META-INF
└── persistence.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | *.iml
3 | target
4 | dependency-reduced-pom.xml
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Dropwizard with Guice and JPA Example
2 | =====================================
3 |
4 | Dropwizard application using Guice and JPA
5 |
6 | Check out: [http://kielczewski.eu/2013/05/developing-restful-web-services-using-dropwizard-part-ii/](http://kielczewski.eu/2013/05/developing-restful-web-services-using-dropwizard-part-ii/) and [http://kielczewski.eu/2013/05/developing-restful-web-services-using-dropwizard-part-iii/](http://kielczewski.eu/2013/05/developing-restful-web-services-using-dropwizard-part-iii/)
7 |
8 | Requirements
9 | ------------
10 | * [Java Platform (JDK) 7](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
11 | * [Apache Maven 3.x](http://maven.apache.org/)
12 |
13 | Quick start
14 | -----------
15 | 1. `mvn package`
16 | 2. `java -jar target/example-dropwizard-guice-1.0-SNAPSHOT.jar`
17 | 3. Point your browser to [http://localhost:8080/hello](http://localhost:8080/hello)
18 |
--------------------------------------------------------------------------------
/configuration.yml:
--------------------------------------------------------------------------------
1 | messages:
2 | hello: Hello world!
3 |
4 | database:
5 | driverClass: com.mysql.jdbc.Driver
6 | user:
7 | password:
8 | url: jdbc:mysql://localhost/test
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | eu.kielczewski.example.dropwizard
8 | example-dropwizard-guice
9 | 1.0-SNAPSHOT
10 | Example Dropwizard With Guice Application
11 |
12 |
13 | 0.6.2
14 | 3.0
15 | 2.5.1
16 | 5.1.30
17 | UTF-8
18 | UTF-8
19 |
20 |
21 |
22 |
23 | com.yammer.dropwizard
24 | dropwizard-core
25 | ${dropwizard.version}
26 |
27 |
28 | com.google.inject
29 | guice
30 | ${guice.version}
31 |
32 |
33 | com.google.inject.extensions
34 | guice-persist
35 | ${guice.version}
36 |
37 |
38 | org.eclipse.persistence
39 | eclipselink
40 | ${eclipselink.version}
41 |
42 |
43 | mysql
44 | mysql-connector-java
45 | ${mysql-connector-java.version}
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.apache.maven.plugins
53 | maven-compiler-plugin
54 | 3.1
55 |
56 | 1.7
57 | 1.7
58 |
59 |
60 |
61 | org.apache.maven.plugins
62 | maven-shade-plugin
63 | 2.2
64 |
65 |
66 | package
67 |
68 | shade
69 |
70 |
71 |
72 |
74 | eu.kielczewski.example.ExampleService
75 |
76 |
77 |
78 |
79 | *:*
80 |
81 | META-INF/*.SF
82 | META-INF/*.DSA
83 | META-INF/*.RSA
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/ExampleService.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example;
2 |
3 | import com.google.inject.AbstractModule;
4 | import com.google.inject.Guice;
5 | import com.google.inject.Injector;
6 | import com.google.inject.persist.PersistFilter;
7 | import com.google.inject.persist.jpa.JpaPersistModule;
8 | import com.yammer.dropwizard.Service;
9 | import com.yammer.dropwizard.config.Bootstrap;
10 | import com.yammer.dropwizard.config.Environment;
11 | import eu.kielczewski.example.config.DatabaseConfiguration;
12 | import eu.kielczewski.example.config.ExampleServiceConfiguration;
13 | import eu.kielczewski.example.config.MessagesConfiguration;
14 | import eu.kielczewski.example.dao.UserDao;
15 | import eu.kielczewski.example.dao.UserDaoImpl;
16 | import eu.kielczewski.example.resource.HelloResource;
17 | import eu.kielczewski.example.resource.UserResource;
18 |
19 | import java.util.Properties;
20 |
21 | public class ExampleService extends Service {
22 |
23 | public static void main(String[] args) throws Exception {
24 | new ExampleService().run(args);
25 | }
26 |
27 | @Override
28 | public void initialize(final Bootstrap bootstrap) {
29 | bootstrap.setName("dropwizard-example-guice");
30 | }
31 |
32 | // you probably would like to move this method to separate class
33 | private Injector createInjector(final ExampleServiceConfiguration conf) {
34 | return Guice.createInjector(new AbstractModule() {
35 | @Override
36 | protected void configure() {
37 | // if someone would like to @Inject ExampleServiceConfiguration
38 | bind(ExampleServiceConfiguration.class).toInstance(conf);
39 | // for ExampleResource, which does @Inject MessagesConfiguration
40 | bind(MessagesConfiguration.class).toInstance(conf.getMessages());
41 | // for UserDao
42 | bind(UserDao.class).to(UserDaoImpl.class);
43 | }
44 | }, createJpaPersistModule(conf.getDatabase()));
45 | }
46 |
47 | // you probably would like to move this method to separate class
48 | private JpaPersistModule createJpaPersistModule(DatabaseConfiguration conf) {
49 | Properties props = new Properties();
50 | props.put("javax.persistence.jdbc.url", conf.getUrl());
51 | props.put("javax.persistence.jdbc.user", conf.getUser());
52 | props.put("javax.persistence.jdbc.password", conf.getPassword());
53 | props.put("javax.persistence.jdbc.driver", conf.getDriverClass());
54 | JpaPersistModule jpaModule = new JpaPersistModule("Default");
55 | jpaModule.properties(props);
56 | return jpaModule;
57 | }
58 |
59 | @Override
60 | public void run(final ExampleServiceConfiguration conf, final Environment env) {
61 | Injector injector = createInjector(conf);
62 | env.addFilter(injector.getInstance(PersistFilter.class), "/*");
63 | env.addResource(injector.getInstance(HelloResource.class));
64 | env.addResource(injector.getInstance(UserResource.class));
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/config/DatabaseConfiguration.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.config;
2 |
3 | import javax.validation.constraints.NotNull;
4 |
5 | @SuppressWarnings("UnusedDeclaration")
6 | public class DatabaseConfiguration {
7 |
8 | @NotNull
9 | private String driverClass;
10 | private String user;
11 | private String password;
12 | @NotNull
13 | private String url;
14 |
15 | public String getDriverClass() {
16 | return driverClass;
17 | }
18 |
19 | public void setDriverClass(String driverClass) {
20 | this.driverClass = driverClass;
21 | }
22 |
23 | public String getUser() {
24 | return user;
25 | }
26 |
27 | public void setUser(String user) {
28 | this.user = user;
29 | }
30 |
31 | public String getPassword() {
32 | return password;
33 | }
34 |
35 | public void setPassword(String password) {
36 | this.password = password;
37 | }
38 |
39 | public String getUrl() {
40 | return url;
41 | }
42 |
43 | public void setUrl(String url) {
44 | this.url = url;
45 | }
46 | }
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/config/ExampleServiceConfiguration.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.config;
2 |
3 | import com.yammer.dropwizard.config.Configuration;
4 |
5 | import javax.validation.Valid;
6 | import javax.validation.constraints.NotNull;
7 |
8 | @SuppressWarnings("UnusedDeclaration")
9 | public class ExampleServiceConfiguration extends Configuration {
10 |
11 | @NotNull
12 | @Valid
13 | private MessagesConfiguration messages;
14 |
15 | @NotNull
16 | @Valid
17 | private DatabaseConfiguration database;
18 |
19 | public MessagesConfiguration getMessages() {
20 | return messages;
21 | }
22 |
23 | public void setMessages(MessagesConfiguration messages) {
24 | this.messages = messages;
25 | }
26 |
27 | public DatabaseConfiguration getDatabase() {
28 | return database;
29 | }
30 |
31 | public void setDatabase(DatabaseConfiguration database) {
32 | this.database = database;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/config/MessagesConfiguration.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.config;
2 |
3 | import javax.validation.constraints.NotNull;
4 |
5 | @SuppressWarnings("UnusedDeclaration")
6 | public class MessagesConfiguration {
7 |
8 | @NotNull
9 | private String hello;
10 |
11 | public String getHello() {
12 | return hello;
13 | }
14 |
15 | public void setHello(String hello) {
16 | this.hello = hello;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/dao/UserDao.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.dao;
2 |
3 | import eu.kielczewski.example.model.User;
4 |
5 | public interface UserDao {
6 |
7 | User getById(long id);
8 |
9 | User save(User user);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/dao/UserDaoImpl.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.dao;
2 |
3 | import com.google.inject.persist.Transactional;
4 | import eu.kielczewski.example.model.User;
5 |
6 | import javax.persistence.EntityManager;
7 | import javax.persistence.PersistenceContext;
8 |
9 | public class UserDaoImpl implements UserDao {
10 |
11 | @PersistenceContext
12 | private EntityManager em;
13 |
14 | @Override
15 | public User getById(long id) {
16 | return em.find(User.class, id);
17 | }
18 |
19 | @Override
20 | @Transactional
21 | public User save(User user) {
22 | em.persist(user);
23 | return user;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/model/User.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.model;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.GeneratedValue;
5 | import javax.persistence.GenerationType;
6 | import javax.persistence.Id;
7 | import javax.validation.constraints.NotNull;
8 | import javax.validation.constraints.Size;
9 |
10 | @Entity
11 | public class User {
12 |
13 | @Id
14 | @NotNull
15 | @GeneratedValue(strategy = GenerationType.IDENTITY)
16 | private long id;
17 |
18 | @NotNull
19 | @Size(min = 2, max = 64)
20 | private String name;
21 |
22 | @NotNull
23 | @Size(min = 2, max = 16)
24 | private String login;
25 |
26 | @NotNull
27 | @Size(min = 2, max = 16)
28 | private String password;
29 |
30 | public User() {
31 | }
32 |
33 | public User(long id, String name, String login, String password) {
34 | this.id = id;
35 | this.name = name;
36 | this.login = login;
37 | this.password = password;
38 | }
39 |
40 | public long getId() {
41 | return id;
42 | }
43 |
44 | public void setId(long id) {
45 | this.id = id;
46 | }
47 |
48 | public String getName() {
49 | return name;
50 | }
51 |
52 | public void setName(String name) {
53 | this.name = name;
54 | }
55 |
56 | public String getLogin() {
57 | return login;
58 | }
59 |
60 | public void setLogin(String login) {
61 | this.login = login;
62 | }
63 |
64 | public String getPassword() {
65 | return password;
66 | }
67 |
68 | public void setPassword(String password) {
69 | this.password = password;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/resource/HelloResource.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.resource;
2 |
3 | import eu.kielczewski.example.config.MessagesConfiguration;
4 |
5 | import javax.inject.Inject;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.ws.rs.*;
8 | import javax.ws.rs.core.Context;
9 |
10 | @Path(value = "/hello")
11 | public class HelloResource {
12 |
13 | private final MessagesConfiguration conf;
14 |
15 | @Inject
16 | public HelloResource(final MessagesConfiguration conf) {
17 | this.conf = conf;
18 | }
19 |
20 | @GET
21 | public String sayHello() {
22 | return conf.getHello();
23 | }
24 |
25 | @POST
26 | @Path("/{id}")
27 | public String parameterDemoMethod(String body,
28 | @PathParam("id") long id,
29 | @QueryParam("foo") String foo,
30 | @HeaderParam("X-Auth-Token") String token,
31 | @Context HttpServletRequest request) {
32 | String response;
33 | response = "id = " + id + "\n";
34 | response += "body = " + body + "\n";
35 | response += "foo = " + foo + "\n";
36 | response += "token = " + token + "\n";
37 | response += "ip = " + request.getRemoteAddr() + "\n";
38 | return response;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/eu/kielczewski/example/resource/UserResource.java:
--------------------------------------------------------------------------------
1 | package eu.kielczewski.example.resource;
2 |
3 | import eu.kielczewski.example.dao.UserDao;
4 | import eu.kielczewski.example.model.User;
5 |
6 | import javax.inject.Inject;
7 | import javax.validation.Valid;
8 | import javax.ws.rs.*;
9 | import javax.ws.rs.core.MediaType;
10 |
11 | @Path("/user")
12 | @Produces(MediaType.APPLICATION_JSON)
13 | @Consumes(MediaType.APPLICATION_JSON)
14 | public class UserResource {
15 |
16 | private final UserDao userDao;
17 |
18 | @Inject
19 | public UserResource(final UserDao userDao) {
20 | this.userDao = userDao;
21 | }
22 |
23 | @GET
24 | @Path("/{id}")
25 | public User getUser(@PathParam("id") long id) {
26 | return userDao.getById(id);
27 | }
28 |
29 | @POST
30 | public User addUser(@Valid User user) {
31 | return userDao.save(user);
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/src/main/resources/META-INF/persistence.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | org.eclipse.persistence.jpa.PersistenceProvider
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------