我有一个名为 MiddleTierResources.java 的 Jersey 资源类,它使用 Jersey(具有以下相关导入:javax.ws.rs.*; javax.ws.rs.core.MediaType; javax.ws.rs.core.Response;)
在该类中,方法的注释如下:
@GET
@Path("/users/{userid}")
@Produces({MediaType.APPLICATION_JSON})
public Response getUserById (final @PathParam("userid") String userid) {
Base.open("org.postgresql.Driver", "jdbc:postgresql:app", "user", "pass");
User user = User.findById(Integer.parseInt(userid));
Base.close();
return Response.ok(user).build();
}
在我构建和检测模型类之后,然后启动构建的服务器,当我转到 localhost:9090/users/1 时,出现以下错误:
org.javalite.activejdbc.DBException: failed to determine Model class name,
are you sure models have been instrumented?
然而,奇怪的是,当我简单地启动这个类本身时,它运行以下 Main 方法并且运行得很好!
public static void main(String[] args){
Base.open("org.postgresql.Driver", "jdbc:postgresql:app", "user", "pass");
User user = User.findById(1);
Base.close();
System.out.println("MAIN SAYS: "+user);
}
并成功打印出:
MAIN SAYS: Model: com.myproject.application.models.User, table: 'users',
attributes: {wallet_secret=null, id=1, username=Johnny, phone=null,
updated_at=2014-07-16 16:57:30.901, email=null, wallet_address=null,
created_at=2014-07-16 16:57:30.901, password=1234567890, activation=null,
account_type=null}
所以我真的不明白发生了什么?为什么main方法运行正常,但是在同一个类中,当我作为服务器运行并使用它的方法时,它不起作用?当类中有其他注释时,仪器是否不起作用?
非常感谢您的帮助!:)