Playframework(16)RESTful Example

Playframework(16)RESTfulExample

IusedPlayframeworkforsometimeforscala/java.Thistime,IjustneedtoupgradetheplayframeworkversionandwritesomeRESTfulAPIIguesstohelpmyfriends.Herearesometips:

MysqlConfiguration

https://www.playframework.com/documentation/2.3.8/JavaDatabase

playframeworkisusingboneCPIguess,notdbcporc3p0.Thatisitsowndatabaseconnectionpoolwhichisclaimedbetter.HereissomeCONFexample:

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

DBMigration

http://flywaydb.org/

Iusedflywaythistime,becauseIamusingflywayinotherproject.Icankeepusingflywayinalltheprojects,scala,javaandetc.

OnecoreclassBaseDAO.javaisasfollow:

packagemodels;

importorg.flywaydb.core.Flyway;

importplay.db.DB;

/**

*Createdbycarlon4/1/15.

*/

publicclassBaseDAO{

privatestaticFlywayinitFlyway(){

Flywayflyway=newFlyway();

flyway.setDataSource(DB.getDataSource("test"));

returnflyway;

}

publicstaticvoidcreate(){

Flywayflyway=initFlyway();

flyway.migrate();

}

publicstaticvoidclean(){

Flywayflyway=initFlyway();

flyway.clean();

}

}

BeanMapper

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

IamusingnativeSQLinDAOlayer,IamnotafanofhibernateorEbean.IprefertodirectlyuseSQLtherewiththehelpofDBUtils.Thenifthecolumnnameandpropertynamearedifferent,thenIneedtowritesomecustomerhandlerforbeanmapping.

packagemodels;

importjava.util.Date;

publicclassTask{

publicLongid;

publicStringname;

publicStringdesn;

publicDatestartDate;

publicDateendDate;

}

packagemodels;

importorg.apache.commons.dbutils.BasicRowProcessor;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.ArrayList;

importjava.util.List;

/**

*Createdbycarlon4/1/15.

*/

publicclassTaskRowProcessorextendsBasicRowProcessor{

@Override

publicObjecttoBean(ResultSetrs,Classtype)throwsSQLException{

Taskitem=newTask();

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

returnitem;

}

@Override

public<T>List<T>toBeanList(ResultSetrs,Class<T>type)throwsSQLException{

Listnewlist=newArrayList();

try{

while(rs.next()){

newlist.add(toBean(rs,type));

}

}catch(SQLExceptionex){

thrownewRuntimeException(ex);

}

returnnewlist;

}

}

UsingthemappinginDAOclass

item=runner.query(sql,newBeanHandler<Task>(Task.class,newTaskRowProcessor()),id);

HowtoDoLogging

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

HowtoConfig

https://www.playframework.com/documentation/2.3.8/ProductionConfiguration

Loadtheconfigatruntime,Playwillcheckthecondirectoryitself

>/path/to/bin/project-name-Dconfig.resource=application-local.conf

Playwillloadthefilefromdirectory

>/path/to/bin/project-name-Dconfig.file=/etc/conf/application-local.conf

Playwillloadthecontentfromremote

>/path/to/bin/project-name-Dconfig.url=http://conf.sillycat.com/conf/application-local.conf

HowtoRuntheTestclass

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

https://www.playframework.com/documentation/2.3.8/JavaFunctionalTest

>activator"testOnlymodels.*"

Or

Entrytheactivatorenvfirst

>[sillycat-playrest]$testOnlymodels.TaskDAOTest

Thesampleprojectisnamedsillycat-playrest.

References:

java8

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

playframeworkdoc

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://commons.apache.org/proper/commons-dbutils/examples.html

writingtest

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

相关推荐