![]() Putting this line of code right underneath the controller class definition will allow your controller to automatically convert Scalatra action results to JSON: Now we can add a bit of magic to the FlowersController. Delete the with ScalateSupport part of the class definition, so it looks like this: class FlowersController extends ScalatraServlet Let's slim things down a tiny bit by removing the ScalateSupport, since we don't need HTML templating support for our API. Scalatra allows you to easily add functionality to your controllers by mixing in Scala traits to your class definitions. It's also got a way to handle 404s, and Scalate templating, which we don't need for our API. It's got a "hello world" action mounted on the application's root path get("/"), which can be accessed using an HTTP GET to the path "/". What you start out with is a very simple generated controller. Open up the FlowersController.scala file found in src/main/scala/com/example/swagger/sample. Let's set up a RESTful interface allowing us to browse flowers. So, now we've got a controller for our flower shop. This tells sbt to automatically recompile and reload the application whenever you change a file. You don't want to have to manually recompile your app and restart Jetty whenever you make a code change, so type this at the sbt prompt: This will start jetty on Visit that URL in your browser, and you should see a Hello World application. When sbt finishes setting everything up, you should be able to start the application by typing the following at the sbt prompt (which looks like a ">"). ![]() This can take several minutes when you're doing it for the first time, as you're getting a full Scala development environment, an embedded webserver (jetty), Scalatra itself, and several companion libraries. Sbt will take care of downloading all of Scalatra's dependencies. sbt 0.12.0 will work, just downgrade the version number in the file project/build.properties. You'll need the latest sbt 0.12.1 for this. Change directory into the flowershop folder, and run Scala's simple build tool by typing: cd flowershop Once you answer the last question, a full Scalatra project will be generated. Hit to accept the defaults for the organization, scala_version, and version questions. You'll be asked a series of questions about your project. ![]() Once you've got a JVM installed, along with cs, giter8, and sbt, you'll be able to generate a new Scalatra project. See the notes at the bottom for setting up Eclipse or IntelliJ if you use those IDEs, but you should be able to do this tutorial in any text editor. The easiest way to do this is by following the installation instructions at the Scalatra website. The first thing you'll need to do is install Scalatra. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |