Can't get basic application to connect to database and start. #4788
Replies: 8 comments
-
Please post your |
Beta Was this translation helpful? Give feedback.
-
DWGettingStartedApplication: import io.dropwizard.setup.Environment;
import io.dropwizard.jdbi3.JdbiFactory;
//import org.skife.jdbi;
//import resources.EventResource;
import org.jdbi.v3.core.Jdbi;
public class DWGettingStartedApplication extends Application<DWGettingStartedConfiguration> {
public static void main(final String[] args) throws Exception {
new DWGettingStartedApplication().run(args);
}
@Override
public String getName() {
return "DWGettingStarted";
}
@Override
public void initialize(final Bootstrap<DWGettingStartedConfiguration> bootstrap) {
// TODO: application initialization
}
@Override
public void run(final DWGettingStartedConfiguration configuration,
final Environment environment) {
// TODO: implement application
final JdbiFactory factory = new JdbiFactory();
final Jdbi jdbi = factory.build(environment, configuration.getDataSourceFactory(), "postgresql");
//environment.jersey().register(new UserResource(jdbi));
//final JdbiFactory factory = new JdbiFactory();
//final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "database");
//final EventDao eventDao = jdbi.onDemand(EventDao.class);
//e.jersey().register(new EventResource(eventDao));
//final MyDAO mydao = jdbi.onDemand(MyDAO.class);
environment.jersey().register(new HelloResource());
environment.jersey().register(new HelloJsonResource(jdbi));
}
} HelloJsonResource: package com.javaeeeee.dwstart;
import javax.ws.rs.POST;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.db.DataSourceFactory;
//import io.dropwizard.jdbi3.JdbiFactory;
import javax.validation.Valid;
import org.jdbi.v3.core.Jdbi;
import java.util.List;
//MyDAO mydao = new MyDAO();
@Path("/hello_json")
public class HelloJsonResource {
//@Valid
//@NotNull
//private DataSourceFactory database = new DataSourceFactory();
private Jdbi database;
HelloJsonResource(Jdbi database){
this.database = database;
}
final MyDAO dao = database.onDemand(MyDAO.class);
//public User user = new User();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getJSONGreeting() {
//final MyDAO dao = database.onDemand(MyDAO.class);
List<User> users = dao.getAllUsers();
//return new User(email);
return users;
//return new Greeting("Hello world get!");
}
public static class TestClass {
@JsonProperty("name")
public String name;
}
@POST
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Greeting postJSONGreeting(TestClass test) {
return new Greeting("Hello world post!" + test.name);
}
//curl -X POST -H "Content-Type: application/json" -d '{"name": "linuxize@example.com"}' http://localhost:8080/hello_json
} |
Beta Was this translation helpful? Give feedback.
-
Try pulling the assignment of |
Beta Was this translation helpful? Give feedback.
-
Sorry can you clarify what you mean? |
Beta Was this translation helpful? Give feedback.
-
I get a different error but my code might not be right. private Jdbi database;
final MyDAO dao;
HelloJsonResource(Jdbi database){
this.database = database;
this.dao = database.onDemand(MyDAO.class);
}
|
Beta Was this translation helpful? Give feedback.
-
I have gotten the code to build however this line,
I have placed it within the constructor and outside and still get the same error. |
Beta Was this translation helpful? Give feedback.
-
@rossedwardsus can you share complete project in a repo? |
Beta Was this translation helpful? Give feedback.
-
Someone in the JDBI community ended up figuring it out. It was a simple issue related to using the wrong imports which i didn't even notice i had done. So the code was compiling but there was probably an error being thrown, possibly related to incompatible types, but it was disappearing and instead i was getting the one i mentioned. Once i changed the imports it worked as expected. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hello. I have been going through the getting started guide. When i try to connect to a local postgresql database and start the server i keep getting an admin error. I don't even need the admin as i am just testing the code for possible use in an application.
This is the command i am running,
java -jar target/DWGettingStarted-1.0-SNAPSHOT.jar server dwgettingstarted.yaml
This is the error i am getting,
Most of the code is nothing more then a direct copy from the getting started and various example code i found. So i am at a loss as to what the problem might be.
Beta Was this translation helpful? Give feedback.
All reactions