Dropwizard:“处理您的请求时出错”,用于从数据库中检索数据

时间:2014-01-21 09:18:33

标签: java hibernate dropwizard

当我试图打开

http://localhost:8080/tailor/orders 

应返回数据库中的所有订单。但它产生错误

{"message":"There was an error processing your request. It has been logged (ID fe49a13e76c59894)."}

我无法追查导致此问题的原因。

我正在使用dropwizard进行restful web服务,并使用dropwizard对sqlight数据库进行休眠。

类别:

资源类:

@Path("/tailor")
@Produces(MediaType.APPLICATION_JSON)
public class TailorResource {

OrderDAO orderdao;

public TailorResource(OrderDAO odao) {
    this.orderdao = odao;
}


@GET
@Path("/orders")
@Produces(MediaType.APPLICATION_JSON)
public List<OrderModel> getAllOrders() {
    return orderdao.findAll();
}

@GET
@Path("/orders/{id}")
@Produces(MediaType.APPLICATION_JSON)
public OrderModel getOrderById(@PathParam("id") int id) {
    return orderdao.findById(id);
}
}

OrderDAO类:

public class OrderDAO extends AbstractDAO<OrderModel>{


 public OrderDAO(SessionFactory sessionFactory) {
    super(sessionFactory);
}

  public OrderModel findById(int id) {
   return get(id);
}

public OrderModel create(OrderModel o) {
    return persist(o);
}

public List<OrderModel> findAll() {
    return list(namedQuery("com.yammer.dropwizard.tailor.model.OrderModel.findAll"));
}}

订单类:

@NamedQueries({
@NamedQuery(
    name = "com.yammer.dropwizard.tailor.model.OrderModel.findAll",
    query = "SELECT o FROM OrderModel o"
),
@NamedQuery(
    name = "com.yammer.dropwizard.tailor.model.OrderModel.findById",
    query = "SELECT o FROM OrderModel o WHERE o.ID = :ID"
)
})
@Entity
@Table(name = "Order")
public class OrderModel {

@Id
@GeneratedValue
@Column(name = "o_id")
int ID;

@Column(name = "o_shirt_quantity")
int shirtQuantity;
@Column(name = "o_longshirt_quantity")
int longshirtQuantity;
@Column(name = "o_trouser_quantity")
int trouserQuantity;
@Column(name = "o_coat_quantity")
int coatQuantity;
@Column(name = "o_deliverydate")
Date deliveryDate;
@Column(name = "o_orderdate")
Date orderDate;
@Column(name = "o_shirt_price")
Double shirtPrice;
@Column(name = "o_longshirt_price")
Double longshirtPrice;
@Column(name = "o_trouser_price")
Double trouserPrice;
 @Column(name = "o_coat_price")
Double coatPrice;
@Column(name = "o_totalamount")
Double totalAmount;
@Column(name = "o_discount")
Double discount;
@Column(name = "o_advancedpayment")
Double advancedPayment;
@Column(name = "o_remainingpayment")
Double remainingPayment;
@Column(name = "o_orderstatus")
int orderStatus;
}

数据库配置类:

public class databaseConfiguration extends Configuration {

@Valid
@NotNull
@JsonProperty
DataSourceFactory dbconfigurations = new DataSourceFactory();

public DataSourceFactory getDataSourceFactory() {
    //return dbconfigurations;
    Map<String,String> s=new HashMap<String,String>();
    s.put("hibernate.dialect","Hibernate.SQLightDialect.SQLiteDialect");
    dbconfigurations.setProperties(s);
    return dbconfigurations;
}
}

主要服务类别:

public class TailorApplication extends Application<databaseConfiguration> {

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

private final HibernateBundle<databaseConfiguration> hibernate = new HibernateBundle<databaseConfiguration>(CustomerModel.class) {
     {
    @Override
    public DataSourceFactory getDataSourceFactory(databaseConfiguration configuration) {
        return configuration.getDataSourceFactory();
    }
};

@Override
public void initialize(Bootstrap<databaseConfiguration> bootstrap) {
    // TODO Auto-generated method stub
    bootstrap.addBundle(hibernate);

}

@Override
public void run(databaseConfiguration configuration, Environment environment)
        throws Exception {

      final OrderDAO odao = new OrderDAO(hibernate.getSessionFactory());

    environment.jersey().register(new TailorResource(odao));
}

}

YML文件:

dbconfigurations:
# the name of your JDBC driver
driverClass: org.sqlite.JDBC

# the username
user:

# the password
password:

url: jdbc:sqlite:TailorDB.db

请帮忙吗?

2 个答案:

答案 0 :(得分:1)

确保使用实体填充应用程序类:

private final HibernateBundle<AppConfiguration> hibernateBundle = new HibernateBundle<AppConfiguration>(
            //
            //
            //ADD ENTITIES HERE
            //
            //
            Person.class
            ,Product.class
            ) {
        @Override
        public DataSourceFactory getDataSourceFactory(
                AppConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }
    };

答案 1 :(得分:0)

  1. 从表面上看,您似乎错过了getAllOrders上的@UnitOfWork注释。

  2. 话虽如此,您共享的错误是DW默认提供的外部消息。相反,您应该查看您的Web服务日志以获取确切的错误和精确的堆栈跟踪。如果你使用java -jar path / to / shaded.jar server my.yml在终端中运行你的服务,你应该在控制台上看到精心设计的错误。请分享,以便社区可以提供更好的帮助。