Playframework(16)RESTful Example

Playframework(16)RESTful Example

I used Playframework for sometime for scala/java. This time, I just need to upgrade the play framework version and write some RESTful API I guess to help my friends. Here are some tips:

Mysql Configuration
https://www.playframework.com/documentation/2.3.8/JavaDatabase
play framework is using boneCP I guess, not dbcp or c3p0. That is its own database connection pool which is claimed better. Here is some CONF example:
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/playrest"
db.default.user=playrest
db.default.password="playrest"
db.default.logStatements=true

db.test.driver=com.mysql.jdbc.Driver
db.test.url="jdbc:mysql://localhost/test"
db.test.user=tester
db.test.password="tester"
db.test.logStatements=true

DB Migration
http://flywaydb.org/
http://sillycat.iteye.com/blog/2022462

I used flyway this time, because I am using flyway in other project. I can keep using flyway in all the projects, scala, java and etc.
One core class BaseDAO.java is as follow:
package models;

import org.flywaydb.core.Flyway;
import play.db.DB;

/**
* Created by carl on 4/1/15.
*/
public class BaseDAO {

    private static Flyway initFlyway(){
        Flyway flyway = new Flyway();
        flyway.setDataSource(DB.getDataSource("test"));
        return flyway;
    }

    public static void create(){
        Flyway flyway = initFlyway();
        flyway.migrate();
    }

    public static void clean(){
        Flyway flyway = initFlyway();
        flyway.clean();
    }

}

Bean Mapper
http://stackoverflow.com/questions/15322567/how-to-transform-dbutils-resultset-into-javabeans-composited-from-more-domain-ob

I am using native SQL in DAO layer, I am not a fan of hibernate or Ebean. I prefer to directly use SQL there with the help of DBUtils. Then if the column name and property name are different, then I need to write some customer handler for bean mapping.
package models;
import java.util.Date;
public class Task {
    public Long id ;
    public String name;
    public String desn;
    public Date startDate;
    public Date endDate;
}
package models;

import org.apache.commons.dbutils.BasicRowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by carl on 4/1/15.
*/
public class TaskRowProcessor extends BasicRowProcessor {

    @Override
    public Object toBean(ResultSet rs, Class type) throws SQLException {
        Task item = new Task();
        item.id = rs.getLong("ID");
        item.name = rs.getString("NAME");
        item.desn = rs.getString("DESN");
        item.startDate = rs.getTimestamp("START_DATE");
        item.endDate = rs.getTimestamp("END_DATE");
        return item;
    }

    @Override
    public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException {
        List newlist = new ArrayList();
        try {
            while (rs.next()) {
                newlist.add(toBean(rs, type));
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return newlist;
    }


}

Using the mapping in DAO class
item = runner.query(sql,new BeanHandler<Task>(Task.class, new TaskRowProcessor()),id);


How to Do Logging
https://www.playframework.com/documentation/2.3.8/JavaLogging
https://www.playframework.com/documentation/2.3.8/ProductionConfiguration
https://www.playframework.com/documentation/2.3.8/SettingsLogger

How to Config
https://www.playframework.com/documentation/2.3.8/ProductionConfiguration
Load the config at runtime, Play will check the con directory itself
>/path/to/bin/project-name -Dconfig.resource=application-local.conf

Play will load the file from directory
>/path/to/bin/project-name -Dconfig.file=/etc/conf/application-local.conf
  
Play will load the content from remote
>/path/to/bin/project-name -Dconfig.url=http://conf.sillycat.com/conf/application-local.conf

How to Run the Test class
https://www.playframework.com/documentation/2.3.8/JavaTest
https://www.playframework.com/documentation/2.3.8/JavaFunctionalTest

> activator "testOnly models.*"

Or

Entry the activator env first
> [sillycat-playrest] $ testOnly models.TaskDAOTest

The sample project is named sillycat-playrest.

References:
java8
http://www.jooq.org/java-8-and-sql

playframework doc
https://www.playframework.com/documentation/2.3.x/JavaDatabase

dbutils
http://tianyongwei.logdown.com/posts/243610-commons-dbutils
http://aofengblog.blog.163.com/blog/static/63170212014510105657292/
http://wallimn.iteye.com/blog/1606930
http://commons.apache.org/proper/commons-dbutils/examples.html

writing test
https://www.playframework.com/documentation/2.3.8/JavaTest

你可能感兴趣的:(playframework)