Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
play-scala-seed
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COSC360 in 2018
play-scala-seed
Commits
7daa252b
Commit
7daa252b
authored
6 years ago
by
Will Billingsley
Browse files
Options
Downloads
Patches
Plain Diff
Added Play JSON libraries and examples
parent
c75f277f
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/controllers/ExamplesController.scala
+65
-0
65 additions, 0 deletions
app/controllers/ExamplesController.scala
build.sbt
+3
-0
3 additions, 0 deletions
build.sbt
conf/routes
+5
-0
5 additions, 0 deletions
conf/routes
with
73 additions
and
0 deletions
app/controllers/ExamplesController.scala
0 → 100644
+
65
−
0
View file @
7daa252b
package
controllers
import
javax.inject._
import
scala.util.Random
import
play.api.libs.json.
{
JsValue
,
Json
}
import
play.api.mvc._
@Singleton
class
ExamplesController
@Inject
()(
cc
:
ControllerComponents
)
extends
AbstractController
(
cc
)
{
/**
* A controller action that receives JSON as input.
* The bodyParser parses the content for you, and leaves the parsed JSON in req.body
*
* @return
*/
def
receiveJsonPost
()
:
Action
[
JsValue
]
=
Action
(
bodyParser
=
parse
.
json
)
{
req
=>
val
json
:
JsValue
=
req
.
body
/*
* If the JSON is { name: "Fred" }
* then this gets the value that's in name
*/
val
name
=
(
json
\
"name"
).
as
[
String
]
Ok
(
s
"Hello $name"
)
}
/**
* A controller that outputs some JSON
* @return
*/
def
returnJson
()
:
Action
[
AnyContent
]
=
Action
{
req
=>
Ok
(
Json
.
obj
(
"Hello"
->
Seq
(
"World"
,
"Bob"
,
"Alice"
),
"Goodbye"
->
Json
.
obj
(
"Mr"
->
"Chips"
)
))
}
/**
* A controller that takes an optional id in the query string.
* If we get an ID, we return it. If not, we're just allocating a random ID and then
* redirecting so we have an ID.
* @param id
* @return
*/
def
game
(
id
:
Option
[
String
])
=
Action
{
req
=>
def
randomId
=
Random
.
alphanumeric
.
take
(
8
).
toString
id
match
{
case
Some
(
s
)
=>
Ok
(
s
"It was $s"
)
case
None
=>
Redirect
(
"/examples/call"
,
Map
(
"id"
->
Seq
(
randomId
)))
}
}
}
This diff is collapsed.
Click to expand it.
build.sbt
+
3
−
0
View file @
7daa252b
...
...
@@ -12,6 +12,9 @@ resolvers += "Hopper releases" at "http://hopper.une.edu.au/artifactory/libs-rel
resolvers
+=
"Hopper snapshots"
at
"http://hopper.une.edu.au/artifactory/libs-snapshot"
libraryDependencies
+=
guice
libraryDependencies
+=
"com.typesafe.play"
%%
"play-json"
%
"2.6.10"
libraryDependencies
+=
"org.scalatestplus.play"
%%
"scalatestplus-play"
%
"3.1.2"
%
Test
// Adds additional packages into Twirl
...
...
This diff is collapsed.
Click to expand it.
conf/routes
+
5
−
0
View file @
7daa252b
...
...
@@ -6,5 +6,10 @@
# An example controller showing a sample home page
GET / controllers.HomeController.index
GET /example/json controllers.ExamplesController.returnJson()
POST /example/json controllers.ExamplesController.receiveJsonPost()
GET /example/game controllers.ExamplesController.game(id:Option[String])
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment