1

I'm using JavaLite ActiveJDBC to pull data from a local MySQL server. Here is my simple RestController:

@RequestMapping(value = "/blogs")
@ResponseBody
public Blog getAllBlogs( )
    throws SQLException {

    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    List<Blog> blogs = Blog.where( "postType = 'General'" ) ;
    return blogs.get( 0 ) ;

}

And here's my simple model, which extends the ActiveJDBC Model class:

public class Blog
extends Model {

}

Now, here's the problem:when I navigate to the path handled by the controller, I get this output stream:

{"frozen":false,"id":1,"valid":true,"new":false,"compositeKeys":null,"modified":false,"idName":"id","longId":1}

I can tell that this is metadata about the returned objects because the number of these clusters changes based on my parameters - i.e., when I select all, there are four, when I use a parameter, I get the same number as meets the criteria, and only one when I pull the first. What am I doing wrong? Interestingly, when I revert to an old-school DataSource and use the old Connection/PreparedStatement/ResultSet, I'm able to pull data just fine, so the problem can't be in my Tomcat's context.xml or in the path of the Base.open.

rainydaymatt
  • 594
  • 3
  • 10
  • 21

2 Answers2

0

ActiveJDBC models can't be dumped out as raw output streams. You need to do something like this to narrow your selection down to one model, and then refer to its fields.

Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
List<Blog> blogs = Blog.where( "postType = 'General'" ) ;
Blog tempBlog = blogs.get( 0 ) ;
return (String)tempBlog.get( "postBody" ) ;
rainydaymatt
  • 594
  • 3
  • 10
  • 21
  • Actually you can run into issues here because this: ```return (String)tempBlog.get( "postBody" ) ;``` can throw ClassCastException. You need: ```return tempBlog.getString( "postBody" ) ;``` – ipolevoy Nov 23 '15 at 16:21
0

as you stated already, a model is not String, so dumping it into a stream is not the best idea. Since you are writing a service, you probably need JSON, XML or some other form of a String. Your alternatives are:

JSON:

public Blog getAllBlogs() throws SQLException {
    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    String json = Blog.where( "postType = 'General'" ).toJson(false); ;
    Base.close();
    return json ;
}

XML:

public Blog getAllBlogs() throws SQLException {
    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    String xml = toXml(true, false);
    Base.close();
    return xml;
}

Maps:

public Blog getAllBlogs() throws SQLException {
    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    List<Map> maps = Blog.where( "postType = 'General'" ).toMaps() ;
    String output =  doWhatYouNeedToGenerateOutput(maps);
    Base.close();
    return output;
}

Additionally, I must say you are not closing the connection in your code, and generally opening connections inside methods like this is not the best idea. Your code will be littered by connection open and close statements in each method. Best to use a Servlet filter to open and close connections before/ after specific URIs.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • I agree - this is just a simple proof of concept project method I'm using to learn ActiveJDBC. Interestingly, the servlet filter idea is what I'm working on at the moment. – rainydaymatt Oct 30 '15 at 16:15
  • you might want to check out ActiveWeb: http://javalite.io/activeweb. Database connection is already handled there: http://javalite.io/database_configuration and https://github.com/javalite/activeweb-simple/blob/master/src/main/java/app/config/AppControllerConfig.java#L32 – ipolevoy Oct 30 '15 at 18:25