0

I have a Jersey resource class called MiddleTierResources.java that uses Jersey (has the following related imports: javax.ws.rs.*; javax.ws.rs.core.MediaType; javax.ws.rs.core.Response;)

And in that class, methods are annotated like this:

@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();
}

After I build & instrument the model classes, and then fire up the built server, and when I go to localhost:9090/users/1, the following error shows up:

org.javalite.activejdbc.DBException: failed to determine Model class name,
are you sure models have been instrumented?

However, the strange thing is that when I simply fire up this class itself, it runs the following Main method and runs just fine!

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);
}

and successfully prints out:

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}

So I really don't understand what's happening? Why is it that the main method runs properly, but in the same class when I run it as a server and use its methods, it doesn't work? Does instrumentation not work when there are other annotations in the class?

Thanks a lot for you help! :)

Hongyi Li
  • 1,059
  • 2
  • 11
  • 19

1 Answers1

1

I'm pretty sure that Jersey annotations have nothing to do with the problem. Both executions are using different classpaths. The message is only telling you that in the app container you packaged non-instrumented classes. Please, ensure that the app you are deploying includes instrumented classes. Also, another observation: you are opening and closing a connection inside a business method. This will approach will force you to write a lot of code.I suggest you implement a Servlet Filter to do this.. Then you can remove all code to open and close connections from your classes. Here is an example of such a filter: https://github.com/javalite/activejdbc/blob/master/activejdbc/src/main/java/org/javalite/activejdbc/web/ActiveJdbcFilter.java

I hope it helps

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Thanks igor! That makes a lot of sense. Question: my gradle build file currently does clean > build > instrument. So to package instrumented classes, do I have to do a instrument > build instead? – Hongyi Li Jul 22 '14 at 22:53
  • This: `gradle clean build jar`, should produce a jar file with instrumented classes – ipolevoy Jul 23 '14 at 16:26