Guide to making a REST API with Play

2,251 views
Skip to first unread message

Will Sargent

unread,
Sep 18, 2016, 4:37:11 PM9/18/16
to Play Framework
Hi all,

There's a new multi-part guide out on developer.lightbend.com about putting together a REST API using Play.  Part one is out now -- it covers using the routing DSL for specific resources, using a custom action, and leveraging async to map futures from the action all the way to a backend repository:


Will.

Julien L.

unread,
Sep 19, 2016, 8:38:38 AM9/19/16
to Play Framework
Hi,

It's a great initiative to provide a basic REST sample. 

I would like to make you some feedback to improve the sample:
 - You should follow the official scala style guide.
 - I doesn't know any REST API that works with form-urlencoded to post data (OK, actually I know some one...). But JSON is much a much more common and "modern" use case.
 - Sometimes you use "logger.isTraceEnabled())", sometimes no. Lightbend has a good lib for that: https://github.com/typesafehub/scala-logging ;)
 - The Singleton is defined two times for PostRepository (module and implem)
 - Your RequestHandler seems very tied to a specific use case, common REST API doesn't need that.
 - I know this is controversial, but compile time DI is really easy to use with this use case, no reason to depends on runtime DI

Niels Boldt

unread,
Sep 19, 2016, 4:21:51 PM9/19/16
to play-fr...@googlegroups.com
Hi,

Great initiative and the gatling example is pretty cool.

Got one question, why use the PostRouter? As far as I can see the three routes contained in the PostRouter could easily be contained in the normal routes file

Thanks
Niels




--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/c817f4f9-1d00-4f48-ba7e-759190d4b393%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
BinaryConstructors ApS
Vestergade 10a, 4th
1456 Kbh K
Denmark
phone: +4529722259
web: http://www.binaryconstructors.dk
mail: n...@binaryconstructors.dk
skype: nielsboldt

Will Sargent

unread,
Sep 19, 2016, 11:56:54 PM9/19/16
to play-fr...@googlegroups.com
> Got one question, why use the PostRouter? As far as I can see the three routes contained in the PostRouter could easily be contained in the normal routes file

First reason is that a REST API can have multiple combinations of parameters, and it's easier to pull them out from SIRD than it is to specify a custom QueryParamsBindable encompassing the different parameters.

Second reason is that many people using a REST API like to see a "Spray-like" API to map their routing directly to controller logic in the same class.  I used to point people to https://beachape.com/blog/2015/07/25/slim-play-app/ but I would rather show the integration of both routes and SIRD, when each is appropriate.

Will Sargent

unread,
Sep 20, 2016, 12:07:43 AM9/20/16
to play-fr...@googlegroups.com
On Mon, Sep 19, 2016 at 5:38 AM, Julien L. <yotsu...@gmail.com> wrote:
Hi,

It's a great initiative to provide a basic REST sample. 

I would like to make you some feedback to improve the sample:
 - You should follow the official scala style guide.

Okay.
 
 - I doesn't know any REST API that works with form-urlencoded to post data (OK, actually I know some one...). But JSON is much a much more common and "modern" use case.

 
 - Sometimes you use "logger.isTraceEnabled())", sometimes no. Lightbend has a good lib for that: https://github.com/typesafehub/scala-logging ;)

scala-logging is also a wrapper around SLF4J, and would add another library dependency, without adding significant advantages.  
 
 - The Singleton is defined two times for PostRepository (module and implem)

Good catch -- this is a carryover from where the repository was defined in a different subproject.
 
 - Your RequestHandler seems very tied to a specific use case, common REST API doesn't need that.

Sorry, I don't quite follow. 
 
 - I know this is controversial, but compile time DI is really easy to use with this use case, no reason to depends on runtime DI

This is an introductory guide, so I don't want people to have to learn too many new things at once.  There are many companies for whom DI itself is a new concept, let alone compile time DI.
 

Thibault Kruse

unread,
Mar 28, 2017, 8:04:55 PM3/28/17
to Play Framework
Starting with Play for a REST API, what I'd like is a guide for Java as well, and covering Json Binding, Error Handling, Bean Validation and Swagger.

I also wonder about binding using the Form class. That one seems to be intended to be used for HTML forms, so using it for mere http data binding looks like abuse.

Will Sargent

unread,
Mar 29, 2017, 1:06:48 PM3/29/17
to play-fr...@googlegroups.com

--
Will Sargent
Engineer, Lightbend, Inc.


On Tue, Mar 28, 2017 at 5:04 PM, 'Thibault Kruse' via Play Framework <play-fr...@googlegroups.com> wrote:
Starting with Play for a REST API, what I'd like is a guide for Java as well, and covering Json Binding, Error Handling, Bean Validation and Swagger.

I also wonder about binding using the Form class. That one seems to be intended to be used for HTML forms, so using it for mere http data binding looks like abuse.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.

Will Sargent

unread,
Mar 29, 2017, 1:08:54 PM3/29/17
to play-fr...@googlegroups.com
I think there's a couple of different Swagger modules, and that's not core Play functionality, but a module you add on top.

--
Will Sargent
Engineer, Lightbend, Inc.


On Wed, Mar 29, 2017 at 10:06 AM, Will Sargent <will.s...@lightbend.com> wrote:

--
Will Sargent
Engineer, Lightbend, Inc.

Thibault Kruse

unread,
Mar 29, 2017, 10:42:06 PM3/29/17
to Play Framework
I don't see Bean validation applied in the given Java example on github.
Also I don't see Error Handling, what I expect is something that translates Java Exceptions into a Client Response with configurable Http Status and body.

For Swagger, I understand it requires a plugin outside the control of lightbend. So if such plugins do not provide any Java documentation, it is a strategic decision for lightbend whether to fill that gap or not. As a Java user, all I can say is that I miss that documentation.


On Thursday, March 30, 2017 at 2:08:54 AM UTC+9, Will Sargent wrote:
I think there's a couple of different Swagger modules, and that's not core Play functionality, but a module you add on top.

--
Will Sargent
Engineer, Lightbend, Inc.


On Wed, Mar 29, 2017 at 10:06 AM, Will Sargent <will.s...@lightbend.com> wrote:

--
Will Sargent
Engineer, Lightbend, Inc.

On Tue, Mar 28, 2017 at 5:04 PM, 'Thibault Kruse' via Play Framework <play-fr...@googlegroups.com> wrote:
Starting with Play for a REST API, what I'd like is a guide for Java as well, and covering Json Binding, Error Handling, Bean Validation and Swagger.

I also wonder about binding using the Form class. That one seems to be intended to be used for HTML forms, so using it for mere http data binding looks like abuse.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Will Sargent

unread,
Mar 30, 2017, 3:54:56 PM3/30/17
to play-fr...@googlegroups.com
> I don't see Bean validation applied in the given Java example on github.

There's some bean validation examples in https://playframework.com/download#examples -- the documentation is here:


> Also I don't see Error Handling, what I expect is something that translates Java Exceptions into a Client Response with configurable Http Status and body:


> For Swagger, I understand it requires a plugin outside the control of lightbend. So if such plugins do not provide any Java documentation, it is a strategic decision for lightbend whether to fill that gap or not. As a Java user, all I can say is that I miss that documentation.

Lightbend does have already have system for managing microservices APIs, Lagom, so I would start there -- https://github.com/lagom/lagom/issues/280 for a lightbend supported integration between Lagom and Swagger.

Play's an open source framework, and there are already integrations from the community -- four of them at last count.  You can see the different Swagger implementations here:


Although Zalando has renamed their swagger integration from play-swagger to api-first-hand: 


--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/f6af82cf-fa49-4fab-a0a2-fa56cb03c0e3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages