When I let that go through my mind I was in the intensive care unit because of Covid-19, I was without breath for three days and the nurse had brought my wrapped phone to me and I was looking to the…
Spark is the quickest and easiest way to start a simple web server and expose some resources. A simple one-liner can get the job done with spark. It’s that easy!
All you need to get started are the spark binaries in your classpath. As I’m using gradle for build management, all I have to do is add the following line in my build.gradle
file.
With that, you are all set to use spark. Let’s get started.
Open a Java class file and follow along. I’m creating a class called Server
with the main method inside it.
Add a import for all the static methods in spark with spark.Spark.*
. This keeps the very readable.
Let’s start with a simple rest endpoint. A GET
on /hello
should return Hello World
. Pretty simple, right? With any other framework you might use, there would be atleast a couple of lines. But not with spark. All you need to add is,
Now, if you want to pass a path parameter to the above get request after /hello
, say a name, i.e., /hello/selena
or /hello/taylor
, it cab be done with /hello/:name
. The :name
becomes the path parameter here and you can access the variable passed in with request.params(:name)
.
Alright, Now that you’ve got your feet wet in Spark, it is time to take a leap and swim. Let’s build an application through which you can upload and download files.
These are the four endpoints I want to create.
To store the files, I will create a directory called storage
and add all the uploaded files to that.
And I’ve setup the endpoints like this:
From each endpoint, I call a method with the required parameters. downloadFile()
and deleteFile()
only need the file name. countFiles()
doesn't need any argument and uploadFile()
would need the full req
object.
Let’s see the methods now:
I have put effort in making sure that the methods are well-readable on their own. And any lingering doubts should be taken care of by the comments. But of-course, you can always reach out on twitter for any queries.
I’m uploading a LICENSE
file and as you see, it is successfully uploaded.
Let’s confirm that with the count
endpoint.
And we have 1
file which is the LICENSE file we just uploaded.
Now, Let’s try downloading the LICENSE file.
And we have downloaded the file. One thing you’d notice is that we’re getting all of the file text as a single line. This is of course to be expected, as in the downloadFile()
method I have .collect(Collectors.joining())
which joins all the lines together with out adding anything between them. And that is why we get everything in a single line. If you want the line separator character between the lines, then put that in the joining()
method.
And finally let’s test the delete
endpoint.
And the file is deleted. To confirm this let’s run the count once more.
And we’ve got zero indicating that the file is indeed deleted.
And with that, we have all of our endpoints working as expected.
The full code is available as gist at
Thanks for reading. See you again in the next article.
Most businesses buy Google Business Reviews because they know the effects of word of mouth! In the case of your business reputation, what people are saying about you can make or break your business…
Esistono diversi studi anche molto seri riguardo alla soddisfazione. Diciamo che esiste un problema di fondo che tante persone non considerano. Il punto chiave è capire se in questo momento tu ti…
Carrying on from our earlier post about customizing your Listing Gallery, this post will talk about customizing your Listing Detail pages. Our Google™ Analytics data shows that more than a third of…