REST client scala script
This post is written in English, because I read one article recently that says English is one of the key (programming) languages you need to master. I cannot agree more, :). English is very important for progammers, especially if you want to be a top programmer in specific area. There are much more great resources in English than in other languages, so why not start to write some posts or articles in English and make yourself comfortable with reading, writing and thinking? I appologize for any readers of this blog who do not master English well, but I have to do it for me and for you, because anyhow we need improve ourselves up to the next level and it’s almost impossible without English. Get used to it and you’ll be happy with it soon.
At the end of Chapter 2 of Scala in action, there is one example used to revisit Scala features introduced in this chapter. It is called HTTP REST client that can communicate with server side via RESTful requests and responses. In the last post, I have given the method to setup a simple servlet to handle HTTP requests and here I copy the complete example servlet from the book and list it as below,
|
|
We have this servlet running at server side with the help of Gradle and its Jetty plugin. So before we move to client side, we need first make sure it works fine by openning a browser to test it with a url like http://localhost:8080/chapter2/
, the web app name may vary depending on your build directory. Then, a similar response will be shown as follows,
|
|
At the client side, we need to write a Scala script to implement a lightweight RESTful application. Since the complete code has been presented in the book, we just copy it here:
|
|
The first thing I encountered when I was trying to execute this script is classpath, yes, I remember it is also the first thing when I ran my first Java program, it annoyed me at that time. Programmers mainly care about programs instead of environments, but sometimes environment may take us more time than real programming, but we have to face it directly. Scala program(scala) cannot recognize classpath by environment variable $CLASSPATH
by default, so I tried to use the argument -usejavacp
to let it happen, but no luck. Then I ran scala with another argument -classpath
to indicate specific classpath, in our case, it is httpclient’s location. The weird thing is the wildcard “*“ or “*.jar” doen’t work either, so I have to list all the jar files one by one, as shown below,
|
|
Maybe it’s my own environment issue, anyway, I don’t feel scala works well with classpath.
The second thing is the method findIndexOf
is deprecated from Scala 2.9.1, another method called indexWhere
should be used instead, more details can be found at https://github.com/nraychaudhuri/scalainaction/pull/1.
The last thing is the httpclient library. This library is good but it is easily changed from version to version. So don’t panic if you get any deprecation message. Just check the documentation to see the current way of usage.
For the code itself, we need to notice the difference between def
and val
, we can refer to the answer in this thread of Stackoverflow, which explains it clearly: val is evaluated on initialization while def is evaluated only when, and every time, the function is called.
OK, so far so good. Run it and we will get the response. It rocks. :D
|
|
References
- Code samples for Scala in action are available at its official website
- An article about Scala script: Scala as a scripting language